【发布时间】:2013-07-21 03:45:20
【问题描述】:
我正在尝试发送跨域 ajax GET 请求
$(document).ready(function(){
$.ajax
({
type: "GET",
url: "url",
cache: false,
contentType: "application/x-www-form-urlencoded",
dataType: "jsonp",
beforeSend: function (xhr){
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader('Authorization', make_base_auth("login", "pwd"));
},
success: function (data, success){
console.log("success", arguments);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("error", arguments);
},
complete: function(jqxhr, textStatus) {
console.log("complete", arguments);
}
});
});
服务器只接受 application/x-www-form-urlencoded 数据。 发送请求后,我收到 415 状态的响应(不支持的媒体类型) 当我在 firebug 中检查标头时,根本看不到 Content-Type 标头。
GET /url?callback=jsonp1374315852923&_=1374315854258 HTTP/1.1
Host: hostname
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://refferer
Cookie: JSESSIONID=1DCD5D9EB35FEF083C7EAF8F7EA25D54; ltime=1374315850; lid=30000379384174; SESS826f62d511330ecdaa9034d04e399c65=qj2jcempp4osi2hc00lq1jnkp1; AA_SESS=2c-30f9987cd946; AA_UID=0fe8965d05c37706a5bfbec88629ed68
Authorization: Basic YWthdml0YTpQNlBhdQ==
Connection: keep-alive
非常感谢您的 cmets!
【问题讨论】:
-
jsonp 并不是真正的 ajax,它会在 DOM 中插入一个脚本标签,仅此而已,所以除非您联系的服务支持这一点,否则它不会通过设置标题来神奇地工作。跨度>
-
"跨域请求和dataType:"jsonp"请求不支持同步操作。" docu
标签: jquery cross-domain jsonp