繁体   English   中英

需要帮助来制定正确的JSON响应

[英]Need help formulating a correct JSON response

我是使用Jquery ajax调用和json响应的新手,遇到了需要克服的障碍。

我正在使用cleeng开放API ,我想知道我正在使用的其中一个api调用的响应– getRentalOffer()

我正在使用jquery $ ajax()请求,我想进行getRentalOffer()api调用并以JSON格式返回结果。 到目前为止,我的工作是这样(假设以id为参数的POST请求。)

请求:

<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;
?>

尝试此操作时,我收到内部服务器错误。 我试图使用echo json_encode($ offerDetails); 在最后一个echo语句上,然后我没有得到服务器错误。 但是,响应似乎只包含JSON对象的最后一个元素。

我需要帮助来了解如何处理getRentalOffer()的API响应,以便将其作为对$ ajax()请求的正确JSON响应进行传递。

我希望我的问题有道理。 :-)

编辑:使用回声的print_r insead我确实得到了响应文本,但不幸的是出现了错误。 这是文本,在我看来,好像在使用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 ) "

您无法回显数组或对象,请尝试使用

print_r($offerDetails);

要么

var_dump($offerDetails);

解决了。

getRentalOffer()返回的对象包含受保护的成员,这些成员将不受json_encode编码,因为它尊重对象vars中的访问参数。 我在这篇文章中找到了一个不错的解决方案: http : //smorgasbork.com/component/content/article/34-web/65-json-encoding-private-class-members

这不是一个健壮的解决方案,因为它依赖于一个漏洞,有一天可能会关闭,因此请当心。 但是对于我的需求就足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM