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

Php 需要帮助制定正确的JSON响应吗,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我是一个使用jQueryAjax调用和json响应的新手,遇到了需要帮助克服的困难 我正在使用,我想知道我正在使用的一个api调用的响应 我正在使用jquery$ajax()请求,我希望进行getRentalOffer()api调用,并以JSON格式返回结果。到目前为止,我的努力是这样的(假设一个以id为参数的POST请求。) 请求: <script type="text/javascript"> $(document).ready(function() {

我是一个使用jQueryAjax调用和json响应的新手,遇到了需要帮助克服的困难

我正在使用,我想知道我正在使用的一个api调用的响应

我正在使用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>

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

print_r($offerDetails);

解决了

getRentalOffer()返回的对象包含受保护的成员,这些成员不会由json_encode编码,因为它尊重对象变量中的访问参数。我在这篇文章中找到了一个很好的解决方案:


这不是一个稳健的解决方案,因为它依赖于一个有一天可能会被关闭的漏洞,所以要小心。但对于我的需要,这就足够了。

我真的需要一些帮助。:-)使用
echo json\u encode($offerDetails)
而不是
echo$offerDetails
是正确的选择,我想在这里看到您的代码:
$cleengApi->getRentalOffer()
我认为它只传递了一行,而不是行。它确实传递了所有行,我用FireHP检查了$offerDetails以确定。你能粘贴
json\u编码的结果而不是
print\r
?我真的想看看你的
getRentalOffer()
函数。我试着打印,但得到了以下错误:error:SyntaxError:JSON.parse:JSON数据的第1行第1列出现了意外字符。我用回复文本编辑了OP。
"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);