简体   繁体   English

Javascript无法从PHP获取JSON数据

[英]Javascript unable to get JSON data from PHP

I'm having a bit of a problem reading JSON data that I generate in PHP and then pass back to my Javascript and I'm not sure why. 我在读取PHP中生成的JSON数据然后传递回我的Javascript时遇到了一些问题,我不确定为什么。

Here is the PHP: 这是PHP:

header("content-type: text/json");

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
// echo the result as a JSON object
echo json_encode($result);

And here is the Javscript: 这是Javscript:

$.post("payment_do.php", { "token": response.response.token, "ip_address": response.ip_address}).done(function(data) {
    console.log(data);
});

It seems as though if I take the header line away in the PHP I get a response that I can read in the Javascript but I cannot access any of the elements the way you would expect. 好像我在PHP中删除标头行一样,收到了可以在Javascript中读取的响应,但无法按您期望的方式访问任何元素。 If I leave the header in there, I get no response readable by the javascript. 如果我在其中保留标题,则JavaScript无法读取任何响应。

EDIT: Now getting a response from the php, looks like this: 编辑:现在从php得到响应,看起来像这样:

"{\"response\":{\"token\":\"ch_9knTXHoU0dVZsl7iMHyHGg\",\"success\":true,\"amount\":9900,\"currency\":\"AUD\",\"description\":\"test\",\"email\":\"test@test.com\",\"ip_address\":\"1.1.1.1\",\"created_at\":\"2013-03-18T23:49:12Z\",\"status_message\":\"Success!\",\"error_message\":null,\"card\":{\"token\":\"test_token\",\"display_number\":\"XXXX-XXXX-XXXX-0000\",\"scheme\":\"master\",\"address_line1\":\"123 Fake Street Fakington\",\"address_line2\":null,\"address_city\":\"moon\",\"address_postcode\":\"2121\",\"address_state\":\"NSW\",\"address_country\":\"Australia\"},\"transfer\":[],\"amount_refunded\":0,\"total_fees\":999,\"merchant_entitlement\":999,\"refund_pending\":false}}"

In the end I had to remove the json_encode function in the php and just return the result. 最后,我不得不在php中删除json_encode函数,然后返回结果。 In the javascript (using jQuery) I then called: 然后在javascript中(使用jQuery),我调用了:

data = $.parseJSON(data);

With which I could then access the elements of the object. 然后,我可以使用它访问对象的元素。

Change your header to application/json (as opposed to text/json ). 将标头更改为application/json (与text/json相对)。 In addition, if you actually want to further process the results of the curl_exec call then you need to set an additional option: 另外,如果您实际上想进一步处理curl_exec调用的结果,则需要设置一个附加选项:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

This will cause the curl_exec call to return data on success, as opposed to TRUE. 与TRUE相反,这将导致curl_exec调用成功返回数据。

$result = curl_exec($ch);
if(! $result) {
    // handle error
}

echo json_encode($result);

Also, you will want to verify the data you are getting back from the cURL call - make sure that it really needs to be encoded before being returned. 另外,您将要验证从cURL调用中获取的数据-确保在返回之前确实需要对其进行编码。

add this: 添加:

header("Content-Type: application/json");
echo json_encode($result);

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

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