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

查看:102
本文介绍了将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方法不允许),在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数据
  1. the method is OPTIONS not POST
  2. the content-type (in another tab) shows text/html; charset=UTF-8 instead of json
  3. the JSON data is no where to be seen

但是,如果我在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天全站免登陆