向服务器发送 GET 没有响应(但返回状态)
我正在向我的服务器发送一个 ajax 请求,以更新那里的一些数据。请求是通过jquery的get
函数发送的。成功后我希望执行一些操作,但不幸的是整个操作失败,因为服务器没有响应,即使 firebug 清楚地显示 200 状态:
更重要的是,当我用浏览器输入这个地址时,一切看起来都很好。有什么想法可能无法正常工作吗?这是我的js:
$(".interests-delete").live("click", function(){
var id = $(this).attr('title');
var user_id = "100000717262400";
$.ajax({
type: "GET",
url: "http://dev1.gecoloco.com/rte/ilike.php?",
data: "u=" + user_id + "&d=" + id,
dataType: "json",
success: function(response, status){
alert(status);
console.log(response);
getLikes();
}
});
return false;
});
I'm sending an ajax request to my server that updates some data there. Request is sent with jquery's get
function. On success I'd like te perform some action, but unfortunately whole operation fails since server gives no response, even though firebug clearly shows 200 status:
What more, when I enter this address with browser, also everything looks fine. Any ideas why it may not be working properly ? Here's my js:
$(".interests-delete").live("click", function(){
var id = $(this).attr('title');
var user_id = "100000717262400";
$.ajax({
type: "GET",
url: "http://dev1.gecoloco.com/rte/ilike.php?",
data: "u=" + user_id + "&d=" + id,
dataType: "json",
success: function(response, status){
alert(status);
console.log(response);
getLikes();
}
});
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很可能是由于“同源策略”,它要求您的 html 应该来自发出 ajax 调用的同一域。尝试不从文件系统加载您的 html 页面,而是通过服务 ajax 调用的同一服务器加载您的 html 页面。它会起作用。
It's very likely due to "same origin policy' which mandates your html should come from the same domain to which ajax call is issued. Try loading your html page not from file system but through the same server serving the ajax calls. It will work.
你的网址看起来有点有趣。 “?&u=...”部分应该是“?u=...”
Your url looks a bit funny there. The part: "?&u=..." should be "?u=..."
也许你的成功函数出了问题。您可能会收到响应,但您的 JavaScript 在函数中的某个地方崩溃了。
Maybe something is wrong with your success function. It is possible you are getting a response but your javascript is crashing somewhere in your function.
当我在浏览器中从本地文件系统打开页面时,我曾经经历过这种情况,例如
file:///C:/Users/Eason/Desktop/mailer/index.html
。当此页面尝试发出 AJAX 请求时,就会导致该问题。当它发布到 Web 服务器(例如 Tomcat)上时,它就可以工作。
这是浏览器中的安全限制,旨在防止不安全的跨域流量。
I've experienced this before when I opened a page from the local file system in my browser, e.g.
file:///C:/Users/Eason/Desktop/mailer/index.html
.When this page attempts to make an AJAX request, it would cause that problem. When it's published on a web server (e.g. Tomcat) it would work.
This is a security restriction in the browser to prevent unsafe cross-domain traffic.