在我的应用程序中,我有这个代码。我注意到,该内容类型无法发送。你知道怎么强制发送吗?
$.ajax({
crossDomain: true,
type: 'GET',
url: 'http://serv/services/rest/contact/' + localStorage.getItem('contact'),
callback: 'jsonpCallback',
jsonpCallback: 'jsonpCallback',
jsonp: '_jsonp',
**contentType: 'application/json',**
dataType: 'jsonp json',
timeout : 10000,
success: function(data){
$("#name").attr("value", data.response.label);
} },
error: function (xhr, ajaxOptions, thrownError){
alert("Status: " + xhr.status + ", Ajax option: " + ajaxOptions + ", Thrown error: " + thrownError);
},
}); 我的请求头是:
Accept:*/*
Accept-Charset:ISO-8859-2,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Cookie:JSESSIONID=F0ED33279488888888B35A731B40EE0C; oam.Flash.RENDERMAP.TOKEN=789456321
Host:serv
User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1就像我说的contentType失踪了。我遗漏了什么?
谢谢你的帮助。
发布于 2012-08-30 15:26:52
如果您是POSTing数据,则只能设置内容类型。因此,您需要更改:
type: 'POST'您可能希望告诉服务器您接受的响应try:
accepts: 'application/json'有关更多信息,请参阅此处jQuery Ajax API
发布于 2012-08-30 15:35:38
请求不需要内容类型。
将dataType更改为json,如下所示:
$.ajax({
type: "GET",
url: 'http://serv/services/rest/contact/' + localStorage.getItem('contact'),
dataType: "json",
success: function(data){
$("#name").attr("value", data.response.label);
},
error: function (xhr, ajaxOptions, thrownError){
alert("Status: " + xhr.status + ", Ajax option: " + ajaxOptions + ", Thrown error: " + thrownError);
}
});相反,将您的数据作为JSON返回。
我确实注意到了你的crossDomain: true,,为了让它起作用,你需要阅读Cross-Origin Resource Sharing,更确切地说,在响应中添加这个头Access-Control-Allow-Origin: http://www.example.com。
以PHP为例:
header('Access-Control-Allow-Origin: http://www.example.com');.htaccess示例
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "http://www.example.com"
</IfModule>https://stackoverflow.com/questions/12191810
复制相似问题