I want to access a REST service on another domain. If, in JQuery, I specify:
我想访问另一个域上的REST服务。如果,在JQuery中,我指定:
dataType: 'json'
it fails, as expected, since for cross-domain calls, JSONP must be used instead.
正如预期的那样,它失败了,因为对于跨域调用,必须使用JSONP。
When I change this to:
当我改为:
dataType: 'jsonp'
it is expected to work, but fails because the server expects application/json
or application/xml
or text/html
, etc., but not */*
, sent by the JSONP request.
它应该工作,但失败,因为服务器期望由JSONP请求发送的application / json或application / xml或text / html等,但不是* / *。
Is there a way to force JQuery to put application/json
in Accept
request header while doing a JSON request?
有没有办法强制JQuery在执行JSON请求时将application / json放入Accept请求头?
3 个解决方案
#1
5
AFAIK jQuery's implementation of JSONP uses a <script>
tag that is injected into the DOM (thus the restriction to the GET verb only) for which you cannot control the Accept
request content type header. The src
of this script
tag is simply pointed to the remote domain url. It is the browser that simply fetches the underlying endpoint sending a regular GET request.
AFAIK jQuery的JSONP实现使用注入DOM的
So if you want to be able to set request headers for cross domain calls you will have to setup a server side script on your domain that will delegate the call to the remote domain (and set the respective headers) and then send the AJAX request to your script.
因此,如果您希望能够为跨域调用设置请求标头,则必须在域上设置服务器端脚本,该脚本将调用委托给远程域(并设置相应的标头),然后将AJAX请求发送到你的脚本。
#2
0
I think you will want to try something along these lines:
我想你会想尝试这些方面的东西:
$.ajax({
headers: {
Accept : "application/json; charset=utf-8",
"Content-Type": "text/plain; charset=utf-8"
}
dataType: 'jsonp',
success : function(response) {
...
}
})
#3
0
This may not be suitable for your use case, but when I have had to do cross-domain AJAX, I would typically just add an additional resource within my domain which would then call the external resource (via cURL or whatever) and return the value to the calling client. In essence you are building a proxy for the AJAX call. It is certainly more overhead, but you may be able to mitigate that by adding a caching layer for such calls.
这可能不适合您的用例,但是当我不得不进行跨域AJAX时,我通常会在我的域中添加一个额外的资源,然后调用外部资源(通过cURL或其他)并返回值到主叫客户端。实质上,您正在为AJAX调用构建代理。它肯定是更多的开销,但您可以通过为此类调用添加缓存层来缓解这种影响。
#1
5
AFAIK jQuery's implementation of JSONP uses a <script>
tag that is injected into the DOM (thus the restriction to the GET verb only) for which you cannot control the Accept
request content type header. The src
of this script
tag is simply pointed to the remote domain url. It is the browser that simply fetches the underlying endpoint sending a regular GET request.
AFAIK jQuery的JSONP实现使用注入DOM的
So if you want to be able to set request headers for cross domain calls you will have to setup a server side script on your domain that will delegate the call to the remote domain (and set the respective headers) and then send the AJAX request to your script.
因此,如果您希望能够为跨域调用设置请求标头,则必须在域上设置服务器端脚本,该脚本将调用委托给远程域(并设置相应的标头),然后将AJAX请求发送到你的脚本。
#2
0
I think you will want to try something along these lines:
我想你会想尝试这些方面的东西:
$.ajax({
headers: {
Accept : "application/json; charset=utf-8",
"Content-Type": "text/plain; charset=utf-8"
}
dataType: 'jsonp',
success : function(response) {
...
}
})
#3
0
This may not be suitable for your use case, but when I have had to do cross-domain AJAX, I would typically just add an additional resource within my domain which would then call the external resource (via cURL or whatever) and return the value to the calling client. In essence you are building a proxy for the AJAX call. It is certainly more overhead, but you may be able to mitigate that by adding a caching layer for such calls.
这可能不适合您的用例,但是当我不得不进行跨域AJAX时,我通常会在我的域中添加一个额外的资源,然后调用外部资源(通过cURL或其他)并返回值到主叫客户端。实质上,您正在为AJAX调用构建代理。它肯定是更多的开销,但您可以通过为此类调用添加缓存层来缓解这种影响。