Closed ansonphong closed 11 years ago
Once I've got this figured out, I'll be able to implement most of Haidy's PHP functions through the pwData service.
UPDATE : By using .then
following the ajax call, and then doing a manual foreach decode, I can get the string, and using $sce
service, make it 'trustable' HTML, but the manual decode step seems like a hack - not best practices.
Do you know a better way to get the AJAX response without manually decoding it?
Here's the new test button which is working with the "hack"
<div class="o_embed" ng-controller="mediaEmbed">
<div>{{oEmbed}}</div>
<div ng-bind-html="oEmbedDecode"></div>
<button ng-click="oEmbedGet('http://www.youtube.com/watch?v=38peWm76l-U')">Get o Embed</button>
</div>
Fixed:) Please get the latest [plugin and theme] and go to http://localhost/wordpress/search/#/o-embed/
First thing, ajax returns need to be returned as ajax, i already put a framework to do that and using it in the postworld_ajax file, so the function would look like this:
function ajax_oembed_get(){
list($response, $args, $nonce) = initAjaxResponse();
$pw_args = $args;
$oEmbed = wp_oembed_get( $pw_args['link_url'] );
header('Content-Type: application/json');
$response['status'] = 200;
$response['data'] = $oEmbed;
echo json_encode($response);
// die ( $oEmbed );
die;
}
The json header line and the json encode line do most of the magic. This way, the return will be a regular string inside the api wrapper to help with error and status tracking.
second thing, and you already got this tough one right, is the html binding. When binding html with ngSanitize, you protect your users against malicious scripts, but since you are trusting the embed function, the right way to go is to use the $sce.trustAsHtml function. using the then function is a must, since it returns only when the response is ready. we are using the $q library all over the app to manage asynchronous calls.
pwData.wp_ajax('ajax_oembed_get', args ).then(
// Success
function(response) {
$scope.oEmbed = $sce.trustAsHtml(response.data); // we are returning the response in a json object that has status, error, data objects, so we are using response.data to retrieve the embed data.
},
// Failure
function(response) {
alert("error");
}
);
Great, thanks! I'll have a look at this. This will help me develop various other AJAX functions.
Hi Michel, I'm having a very strange issue happening which I wanted to ask you about.
So I wrote and implemented the basic methods for Wordpress oEmbed using AJAX, to get the embed code for a given URL - and everything is working quite nicely, I'm getting the expected response via AJAX, and everything is good - except there's one problem.
When I try and print it, it comes in the form of some kind of serialized object, with the embed code as the value of all these keys.
What do I need to do in this case to make it work?
Here is the function so you can look :
js/app.js
I added controller function
mediaEmbed
js/services/pwData.js
I added
ajax_oembed_get
to the service.php/postworld_ajax.php
I added
ajax_oembed_get()
Test Button
You can test it anywhere with this HTML, by clicking the button to see what I'm talking about:
Thanks!!