2012-03-10 86 views
1

我用这jQuery.ajax响应:jQuery的阿贾克斯 - 不能得到

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"} 

有没有人有什么建议?我会认为这很容易......也许是这样,我只是在做一些愚蠢的事情。

感谢

+0

你的浏览器的Javascript错误控制台说什么? – 2012-03-10 23:07:13

+1

您的网页是否与PHP服务器在同一个域上?浏览器的错误控制台或调试控制台是否会报告任何javascript错误或安全错误? – jfriend00 2012-03-10 23:07:32

+0

试过'header(“Content-Type:application/json”);'?你实际上并没有发回纯文本,是吗? – Matijs 2012-03-10 23:11:34

回答

0

似乎您试图调用http://client.my_url.com与AJAX从http://localhost:8080

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

+0

我可以以纯文本方式传输吗JSONP不允许非异步调用,并且我需要其中一个调用为了满足需求,我做了一些改变,使得响应头文件中包含:“Content-Type:text/plain”,并且我在ajax调用中添加了“dataType:'文本',”参数,但似乎仍然没有返回任何东西。:( – JMecham 2012-03-10 23:49:51

+0

它让我花了比我更愿意承认让JSONP工作的时间,但现在是这样。如果其他人最终面临这个问题,请到这里进行更多演练: http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using -jsonp-jquery-and-php/ http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/ – JMecham 2012-03-13 06:51:46