如何使用dataType:'jsonp'但仍然在Accept头中有application / json? [英] How to use dataType: 'jsonp' but still have application/json in Accept header?
问题描述
我想访问另一个域上的REST服务。如果,在JQuery中,我指定:
I want to access a REST service on another domain. If, in JQuery, I specify:
dataType: 'json'
它失败,正如预期的那样,因为对于跨域调用,必须使用JSONP。
it fails, as expected, since for cross-domain calls, JSONP must be used instead.
当我将其更改为:
dataType: 'jsonp'
预计会有效,但失败是因为服务器需要 application / json
或应用程序/ xml
或 text / html
等,但不是 * / *
,已发送通过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.
有没有办法强制JQuery在<$ c $中放入 application / json
c>在执行JSON请求时接受请求标头?
Is there a way to force JQuery to put application/json
in Accept
request header while doing a JSON request?
推荐答案
AFAIK jQuery的JSONP实现使用了< script>
注入DOM的标记(因此仅对GET动词的限制),您无法控制接受
请求内容类型标题。此脚本
标记的 src
只是指向远程域URL。浏览器只是简单地获取发送常规GET请求的基础端点。
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.
因此,如果您希望能够为跨域调用设置请求标头,则必须在您的域上设置服务器端脚本,该脚本将调用委托给远程域(并设置相应的头),然后将AJAX请求发送到您的脚本。
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.
这篇关于如何使用dataType:'jsonp'但仍然在Accept头中有application / json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!