dongya2578
dongya2578
2014-07-24 17:05

AJAX请求接受硬编码的JSON,但不接受软编码

  • json
  • php
  • ajax
  • jquery

This AJAX request returns 'success' if the output in PHP is copied and pasted JSON, but 'fail' if it's generated by the file. Have a look at the comments in api.php below to see what I mean.

$.ajax({
    url: 'api.php',
    dataType: 'JSON',
    success: function() {
        alert('success');
    },
    error: function() {
        alert('fail');
    }
});

api.php:

<?php
header('Access-Control-Allow-Origin: *');

$jsonurl = "http://findyourmp.parliament.uk/api/search?q=st+pancras&f=js";
$json = file_get_contents($jsonurl);

// THIS DOESN'T WORK (RETURNS 'FAIL')
print_r($json);
?>

<!-- THIS WORKS (RETURNS 'SUCCESS') -->
{"results": { "constituencies": [{"constituency_name": "Holborn and St Pancras", "member_name": "Frank Dobson", "member_party": "Labour", "member_biography_url": "http://www.parliament.uk/biographies/commons/Frank-Dobson/180", "member_website": "", "uri": "http://findyourmp.parliament.uk/constituencies/holborn-and-st-pancras.json" } ], "members": [] }}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐