I am trying to consume a WCF Rest Service from javascript using $.ajax, I am aware that there are issues with cross-domain calls. After googling it for more than a day, I have come quite far but the last mile is missing.
我正在尝试使用$ .ajax从javascript使用WCF Rest服务,我知道跨域调用存在问题。谷歌搜索了一天以上,我走得很远,但最后一英里不见了。
First off, here is my client-code:
首先,这是我的客户端代码:
$.ajax({
url: "https://myurl-here/api/test?param1=x¶m2=y&callback=?",
type: 'GET',
dataType: 'json',
headers: {
"Authorization": "Basic xxxxxxxxx"
},
success: function(data) {
alert(data);
},
error: function(data, errorThrown) {
alert(errorThrown);
}
});
Note that I added the callback=?
parameter and set the dataType='json'
. I am debugging this using Chrome, which has some powerful tools. The response from the server is 200 OK
. However, I get an error saying:
请注意,我添加了回调=?参数并设置dataType ='json'。我正在使用Chrome调试这个,它有一些强大的工具。服务器的响应是200 OK。但是,我收到一个错误说:
Uncaught SyntaxError: Unexpected token :
未捕获的SyntaxError:意外的令牌:
After googling some more, I think this error might be because the server respose is set to content-type "application/json" and the client expects "application/javascript". Here is what the debugging tool returns:
谷歌搜索后,我认为这个错误可能是因为服务器respose设置为内容类型“application / json”,客户端期望“application / javascript”。以下是调试工具返回的内容:
If I click on this and go to the response tab, I even see the plain json string from the response, which is correct and just what I want.
如果我点击它并转到响应选项卡,我甚至会从响应中看到普通的json字符串,这是正确的,正是我想要的。
Now my question is: What can I do? Is there a way to tell ajax that I dont expect a script but it should just take the content and parse it as plain json? Or is there a way of ignoring the parse error and get hold of the content afterwards? The chrome debugger tool seems to somehow get hold of the content.
现在我的问题是:我该怎么办?有没有办法告诉ajax我不期望一个脚本,但它应该只是采取内容并解析为普通的json?或者有没有办法忽略解析错误并在之后获取内容? Chrome调试器工具似乎以某种方式获取内容。
Or is there a way to completely get rid of this callback=? which I suspect makes the contentType to jsonp (I'm not entirely sure how this all works).
或者有没有办法完全摆脱这个回调=?我怀疑将contentType设为jsonp(我不完全确定这一切是如何工作的)。
I can also change code on the server side, however there are some constraints: the server side code cannot be hosted in IIS, it is a self-hosted WCF service.
我也可以在服务器端更改代码,但是有一些限制:服务器端代码不能托管在IIS中,它是一个自托管的WCF服务。
1 个解决方案
#1
2
You should try to use:
你应该尝试使用:
dataType: 'jsonp'
and remove
&callback=?
from the URL.
来自URL。
Update:
Update
After receiving the response Jquery tries to validate that the response contains valid JSON. if the response is not valid JSON you will get the parse error.
收到响应后,Jquery尝试验证响应是否包含有效的JSON。如果响应无效JSON,您将收到解析错误。
- If you use dataType : 'json' and the response contains a callback like callback({ /* ... */ }) it will fail because the callback is not considered valid JSON.
- If you use dataType : 'jsonp' jquery will invoke the callback first and then validate the JSON.
如果您使用dataType:'json'并且响应包含回调(如回调({/ * ... * /}),则它将失败,因为回调不被视为有效的JSON。
如果使用dataType:'jsonp',jquery将首先调用回调,然后验证JSON。
You can try to use a validation tool like jsonlint.com to ensure that the returned JSON is valid. If you want to stop Jquery from validating the response, you can set the response content type as text in the server-side. In the clinet-side, and use contentType : 'text'
in the jquery AJAX call. In the success
callback use the JavaScript function JSON.parse(text)
to transform the response text into JSON.
您可以尝试使用jsonlint.com之类的验证工具来确保返回的JSON有效。如果要阻止Jquery验证响应,可以将响应内容类型设置为服务器端的文本。在clinet端,并在jquery AJAX调用中使用contentType:'text'。在成功回调中,使用JavaScript函数JSON.parse(text)将响应文本转换为JSON。
If you want your WCF service to support CORS you will need to add JSONP support or enable CORS. If you decide to enable CORS you will need the Jquery ajax setting crossDomain.
如果您希望WCF服务支持CORS,则需要添加JSONP支持或启用CORS。如果您决定启用CORS,则需要Jquery ajax设置crossDomain。
Hope it helps!
希望能帮助到你!
#1
2
You should try to use:
你应该尝试使用:
dataType: 'jsonp'
and remove
&callback=?
from the URL.
来自URL。
Update:
Update
After receiving the response Jquery tries to validate that the response contains valid JSON. if the response is not valid JSON you will get the parse error.
收到响应后,Jquery尝试验证响应是否包含有效的JSON。如果响应无效JSON,您将收到解析错误。
- If you use dataType : 'json' and the response contains a callback like callback({ /* ... */ }) it will fail because the callback is not considered valid JSON.
- If you use dataType : 'jsonp' jquery will invoke the callback first and then validate the JSON.
如果您使用dataType:'json'并且响应包含回调(如回调({/ * ... * /}),则它将失败,因为回调不被视为有效的JSON。
如果使用dataType:'jsonp',jquery将首先调用回调,然后验证JSON。
You can try to use a validation tool like jsonlint.com to ensure that the returned JSON is valid. If you want to stop Jquery from validating the response, you can set the response content type as text in the server-side. In the clinet-side, and use contentType : 'text'
in the jquery AJAX call. In the success
callback use the JavaScript function JSON.parse(text)
to transform the response text into JSON.
您可以尝试使用jsonlint.com之类的验证工具来确保返回的JSON有效。如果要阻止Jquery验证响应,可以将响应内容类型设置为服务器端的文本。在clinet端,并在jquery AJAX调用中使用contentType:'text'。在成功回调中,使用JavaScript函数JSON.parse(text)将响应文本转换为JSON。
If you want your WCF service to support CORS you will need to add JSONP support or enable CORS. If you decide to enable CORS you will need the Jquery ajax setting crossDomain.
如果您希望WCF服务支持CORS,则需要添加JSONP支持或启用CORS。如果您决定启用CORS,则需要Jquery ajax设置crossDomain。
Hope it helps!
希望能帮助到你!