将 JSON 数据从 JQuery 发送到 WCF REST 方法的问题 [英] Problem sending JSON data from JQuery to WCF REST method

查看:26
本文介绍了将 JSON 数据从 JQuery 发送到 WCF REST 方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让 jquery 将一些 json 数据发布到我的 WCF 服务上的 rest 方法时遇到了一些麻烦.

I'm having some trouble getting jquery to post some json data to a rest method I have on my WCF service.

在 WCF 方面,这是操作合同:

On the WCF side, here's the operation contract:

[OperationContract]
[WebInvoke(Method = "POST",
           BodyStyle = WebMessageBodyStyle.Bare,
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "PostSomething")]
MyResult PostSomething(MyRequest request);

MyResultMyRequest 都标有所有必需的 DataContractDataMember 属性,并且服务暴露了 WebHttp端点.

both MyResult and MyRequest are marked with all the necessary DataContract and DataMember attributes and service is exposing WebHttp endpoint.

在 JQuery 方面,这是我的函数调用:

On the JQuery side, here's my function call:

var jsonStr = JSON.stringify(reqObj);

$.ajax({
    type: "POST",
    dataType: "json",
    url: "http://localhost/MyService/PostSomething",
    contentType: "application/json; charset=utf-8",
    data: jsonStr,
    success: function (html) {
        alert(html);
    }
});

这个请求永远不会到达我的方法(我每次都得到一个 405 Method Not Allowed),在 Charles 中查看请求如下所示:

this request never reaches my method (I get a 405 Method Not Allowed everytime), and looking in Charles the request looks like this:

OPTIONS /MyService/PostSomething HTTP/1.1
Host: localhost
Cache-Control: max-age=0
Access-Control-Request-Method: POST
Origin: null
Access-Control-Request-Headers: Content-Type, Accept
Accept: */*
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

有几点很奇怪:

  1. 方法是 OPTIONS 而不是 POST
  2. 内容类型(在另一个选项卡中)显示 text/html;charset=UTF-8 而不是 json
  3. 无法看到 JSON 数据

但是,如果我修改 Charles 中的请求,使其标头类似于解决方案 这里,然后一切正常:

However, if I modify the request in Charles so that its headers is similar to the solution here, then everything works:

POST /MyService/PostSomething HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: localhost
Content-Length: 152

{"Id":"", "Name":"testspot","Description":"test" } 

查看此处的教程和其他问题,其他人已设法让 JQuery 发布到这样的 WCF REST 方法,而我不知道我在这里做错了什么..

looking at tutorials and other questions on here others have managed to get JQuery to post to a WCF REST method like this, and I'm at a loss as to what I'm doing wrong here..

哦,说一下上下文,这是一个 WCF 4 服务,我使用的是 JQuery 1.4.4.

oh, to put some context, this is a WCF 4 service and I'm using JQuery 1.4.4.

谢谢,

更新:

阅读更多内容并感谢 Darrel 向我指出跨域规范后,我设法通过在服务界面上对我的服务进行了一些小的更改来取得更进一步的进展:

After some more reading and thanks to Darrel for pointing me towards the cross-domain spec, I managed to get a bit further by making some small changes to my service, on the service interface:

[OperationContract]
[WebInvoke(Method = "*",
           BodyStyle = WebMessageBodyStyle.Bare,
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "PostSomething")]
MyResult PostSomething(MyRequest request);

在实现中,我需要检查传入的请求是否针对 OPTIONS,在这种情况下返回一些标头而不是执行预期的工作:

and in the implementation, I need to check if the incoming requests is for OPTIONS and in that case return some headers rather than doing the intended work:

if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
{
    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST");
    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");

    return null;
}

然后该方法被调用两次,第一次服务器返回null但向客户端添加了一些标头,然后使用POST作为方法发出实际请求,服务器继续正常处理请求.

the method then gets called twice, the first time the server returns null but adds some headers to the client, and then the actual request is made with POST as method and the server goes ahead and deals with the request normally.

推荐答案

这似乎是 Firefox 避免跨域调用的事情.请参阅 http://www.petefreitag.com/item/703.cfm

This seems to be a Firefox thing for avoiding cross domain calls. See http://www.petefreitag.com/item/703.cfm

这里的规范是http://www.w3.org/TR/cors/ 并经过非常简短的阅读后,似乎因为您正在进行跨域调用,您的服务应该实现 OPTIONS 方法并返回一些允许发送 POST 方法的标头.

The spec for this is here http://www.w3.org/TR/cors/ and after a very brief read, it appears that because you are doing a cross domain call, your service is expected to implement the OPTIONS method and return some headers that allow the POST method to be sent.

这篇关于将 JSON 数据从 JQuery 发送到 WCF REST 方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆