【发布时间】:2012-03-10 23:05:47
【问题描述】:
我正在使用这个 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"}
有人有什么建议吗?我认为这会很容易......也许是这样,而我只是在做一些愚蠢的事情。
谢谢
【问题讨论】:
-
您的浏览器的 Javascript 错误控制台说什么?
-
您的网页是否与 PHP 服务器在同一个域中?您的浏览器的错误控制台或调试控制台是否报告任何 javascript 错误或安全错误?
-
试过
header("Content-Type: application/json");?你实际上并没有发回纯文本,是吗? -
不要使用完整的 url 只需使用 /dir/a/b/c/file.php 如果您使用 chrome,请检查日志
-
Javascript 错误控制台 (firefox) 没有错误。有警告,但这些似乎都来自 jquery-ui css 问题。 firebug 中的 net 选项卡的请求标头有:GET /test_get_account_data.php?_=1331419975860 HTTP/1.1 主机:client.sweepscoach.com 用户代理:Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:10.0。 2) Gecko/20100101 Firefox/10.0.2 Accept: / Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Connection: keep-alive Referer: localhost:8080/SCSalesDataReport/test/…来源:localhost:8080