昨天一整天我一直在努力解决这个问题,但事实证明这对我来说非常具有挑战性。
我正在尝试使用这个 JS 从我编写的 java 应用程序中获取信息。
$(document).ready(function() {
$.getJSON('http://localhost/custest?callback=?', function(json) {
alert('OK');
$('.result').html(json.description);
});
});
Java 应用程序使用 httpServer 并且非常基础。
当我访问页面' http://localhost/custest?callback= ?' 使用 Firefox,浏览器向我显示服务器正在向我发送 json 数据并询问用什么打开它,但是当我使用上面的 JS 从网页尝试它时它不起作用。getJSON 调用不成功,alert("ok") 根本不弹出。
如果它替换“ http://localhost/custest?callback= ?” 在带有“ http://twitter.com/users/usejquery.json?callback= ?”的 JS 中 一切正常。
有趣的是,如果我从我的 java 服务器发送格式错误的 JSON,Firebug 会给出错误并告诉我 JSON 中缺少什么,这意味着浏览器正在接收 JSON 数据,但是当我发送正确的 JSON 字符串时,什么也没有发生,没有错误,甚至 alert() 也没有打开。
如果您认为这些可能相关,我将添加标题。
http://localhost/custest?callback=jsonp1274691110349
GET /custest?callback=jsonp1274691110349 HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json
谢谢你的帮助。