簡體   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