违反了同源策略?
heh,
我正在使用jQuery AJAX调用从自己托管的Web服务(同一个域)中提取数据,但它始终返回0,这表示跨域问题。但这不应该是一个问题。违反了同源策略?
任何建议如何解决这个问题?谢谢!
网站上运行我的脚本
http://www.mysite.com/facebook/el_login
我的AJAX调用:
var data = 'username=' + username.val() + '&password=' + password.val()
$.ajax({
url: "http://www.mysite.com/api/v01/account/exists.json",
type: "GET",
data: data,
cache: false,
complete: function(transport) {
if(transport.status == 200) {
alert('Success');
} else {
alert('Failed ' + transport.status);
}
}
});
})
Firebug的请求报头:
Request Headersview source
Host www.mysite.com
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
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
Proxy-Connection keep-alive
Content-Type application/x-www-form-urlencoded
X-Requested-With XMLHttpRequest
Referer http://www.mysite.com/facebook/el_login
Cookie sessionid=xxx
编辑:
好吧,似乎AJAX调用静态网站(同一台服务器)正在工作。我的Webservice后端基于Django,Apache2和mod_wsgi ..也许这是失败的原因。
好了,几个小时后的星际争霸2发现了它。
我有线我的提交按钮与
$('#submit').click(function()
这似乎创造了一些问题与jQuery的Ajax
解决方案:
使用“旧”风格线了你的Ajax调用。
<input type="button" value="Submit" id="submit" onClick="sendLogin()" />
和
function sendLogin() {
var query_data = { username: 'test', password: 'test'};
$.ajax({
url: "http://www.mysite.com/api/v01/account/exists.json",
type: "POST",
data: $.toJSON(query_data),
dataType: 'application/json',
contentType: 'application/json',
complete: function(transport) {
if(transport.status == 200) {
alert('Success');
} else {
alert('Failed ' + transport.status);
}
}
});
}
奇数。如果你使用'$('#formID')。submit(function ...'并且改变输入类型以提交(并返回false)? – Jhong 2010-08-12 08:23:52
对于debuggin我用:
function showResponse(responseText, statusText, xhr, $form) {
debugger; // TODO: Remove in production.
}
,并在Ajax调用使用
error: showResponse
您是否尝试过使用下列参数:
dataType: "json",
contentType: "application/json; charset=utf-8",
我的webservice不需要内容类型,如果我提供一个查询字符串 – 2010-08-11 11:52:10
,但在您的示例中,我没有看到url参数中的查询字符串... – 2010-08-11 11:54:06
他将“数据”属性中的对象传递给jQuery – Pointy 2010-08-11 12:26:34
看起来不错。你确定你总是使用'http://'而不是将它与'https://'混合在一起?没有不同的端口?没有自动删除或添加'www.'? – 2010-08-11 11:31:37
您是否尝试在回调中添加'function(transport,textStatus)'并查看'textStatus'是否提供了更详细的信息? – 2010-08-11 11:32:49
textStatus返回null,表示中止的请求。 – 2010-08-11 11:35:49