jQuery Ajax - 无法获得响应

时间:2012-03-10 23:05:48

标签: php json jquery

我正在使用这个jQuery.ajax:

var url = "http://client.my_url.com/test_get_account_data.php";  
jQuery.ajax({
    type: "GET",
    url: url,
    cache: false,
    success: function(resultsData){
        alert("We're finally making the call.");
    },
    error:function (jqXHR, textStatus, errorThrown){
        alert("Error:" + textStatus+ "," + errorThrown);
    } 
});

点击这个php脚本:

<?php
header("Content-Type: text/plain");

$myFile = "LogFile.log";
$fh = fopen($myFile, 'w');

$accountJSON = array("id"=>"Level 3.accountName","type"=>"Level 3","name"=>"accountName","total"=>"1059.25","in"=>"8603.56","out"=>"7544.31");

$encodedResponse = json_encode($accountJSON);

fwrite($fh, "We're at the end of get_account_data with encodedResponse:\n");
fwrite($fh, $encodedResponse."\n");

echo $encodedResponse;
?>

但出于某种原因,我从未获得成功。我已经尽可能地简化了这个,但它仍然失败了。在日志中,我有输出:

We're at the end of get_account_data with encodedResponse:<br/>
{"id":"Level 3.accountName", "type":"Level 3", "name":"accountName", "total":"1059.25", "in":"8603.56", "out":"7544.31"}

有没有人有任何建议?我认为这将是非常容易的......也许它就是,我只是在做一些愚蠢的事情。

由于

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试使用来自http://client.my_url.com

的AJAX GET请求来调用http://localhost:8080

根据same origin policy浏览器,您无法将AJAX GET / POST请求发送到另一个域。您需要将JSONP用于跨域AJAX。