I am a novice using Jquery ajax calls and json responses and have hit a bump that I need help to overcome.
I am using cleeng open API and I am wondering about the response from one of the api calls I am using – getRentalOffer() .
I am using jquery $ajax() request and I want to make the getRentalOffer() api call and return the result in JSON format. My efforts so far is this (assume a POST request with id as parameter.)
Request:
<script type="text/javascript">
$(document).ready(function() {
$( "#getOfferButton" ).click(function() {
var offerId = document.frm.offerID.value;
$.ajax({
// the URL for the request
url: "ajax-get-offer.php",
// the data to send (will be converted to a query string)
data: {
id: offerId
},
// whether this is a POST or GET request
type: "POST",
// the type of data we expect back
dataType : "json",
// code to run if the request succeeds;
// the response is passed to the function
success: function(json) {
// $("#title").val = json.title;
/*
$.each(json, function(i, item){
$("#"+item.field).val(item.value);
});
*/
console.log(json);
},
// code to run if the request fails; the raw request and
// status codes are passed to the function
error: function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
},
// code to run regardless of success or failure
complete: function( xhr, status ) {
alert( "The request is complete!" );
}
});
});
});
</script>
ajax-get-offer.php:
<?php
include_once('Cleeng-cleeng-php-sdk-fe2a543/cleeng_api.php');
/*
Using FirePHP to log variables
*/
require_once('FirePHPCore/FirePHP.class.php');
ob_start();
$firephp = FirePHP::getInstance(true);
$offerID = $_POST['id'];
$firephp->log($offerID, 'offerID');//For debugging
$publisherToken = 'My super secret token goes here!';
$cleengApi = new Cleeng_Api();
$cleengApi->setPublisherToken($publisherToken);
$offerDetails = $cleengApi->getRentalOffer($offerID);
$firephp->log($offerDetails, 'offerDetails');//For debugging
echo $offerDetails;
?>
When I try this I get Internal server error. I tried to use echo json_encode($offerDetails); on that last echo statement and then I do not get the server error. However the response only seem to contain the last element of the JSON object.
I need help to understand what I need to do with the API response from getRentalOffer() in order to pass it as a proper JSON response to the $ajax() request.
I hope my question make sense. :-)
Edit: Using print_r insead of echo I do get a response text but sadly with an error. This is the text and it looks to me as if it need to be formatted correctly before using print_r.
"Cleeng_Entity_RentalOffer Object ( [id:protected] => R875937249_SE [publisherEmail:protected] => martin.xxxxxxxx@xxxxxxx.se [url:protected] => http://xxx.xxxxxx.xx/cleeng_tool [title:protected] => Tjohooo! [description:protected] => En skön rulle om Afrika. [price:protected] => 55 [applicableTaxRate:protected] => 0.21 [period:protected] => 48 [currency:protected] => EUR [socialCommissionRate:protected] => 0 [contentType:protected] => video [contentExternalId:protected] => xxxxxxxxxxx [contentExternalData:protected] => {"platform":"vimeo","dimWidth":"500","dimHeight":"369","hasPreview":false,"previewVideoId":"","backgroundImage":"https://i.vimeocdn.com/video/xxxxxxxxx_960.jpg"} [contentAgeRestriction:protected] => [tags:protected] => Array ( [0] => abo ) [active:protected] => 1 [createdAt:protected] => 1400588711 [updatedAt:protected] => 1400606512 [pending:protected] => [averageRating] => 4 ) "
You cannot echo arrays or object, try using
print_r($offerDetails);
or
var_dump($offerDetails);
Solved.
The object returned by getRentalOffer() contains protected members which will not be encoded by json_encode because it respects the access parameters in the object vars. I found a nice solution in this post: http://smorgasbork.com/component/content/article/34-web/65-json-encoding-private-class-members
It is not a robust solution as it relies on a loophole that one day might be shut so beware of that. But for my needs it will suffice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.