简体   繁体   English

WCF Ajax 调用不适用于 Jquery $.ajax

[英]WCF Ajax Call not working with Jquery $.ajax

I have the following jQuery (service name altered):我有以下 jQuery(服务名称已更改):

var url = "http://localhost/services/MyService.svc/addentrant";
var stuff = $("#signup-form").serializeArray();

$.ajax({
    type: "POST",
    url: url,
    contentType: "application/json; charset=utf-8",
    data: stuff,
    timeout: 10000,
    success: function (obj) { alert('yay!'); }
});

The above makes a request to a WCF service hosted in Sitefinity on my local IIS7.5 server.上面向我的本地 IIS7.5 服务器上的 Sitefinity 中托管的 WCF 服务发出请求。 Below is the relevant web.config:下面是相关的web.config:

<endpointBehaviors>
<behavior name="jsonBehavior">
  <webHttp/>
</behavior>
...
<serviceBehaviors>
        <behavior name="DefaultBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
...
<services>
 <service behaviorConfiguration="DefaultBehavior" name="Services.MyService" >
        <endpoint address="" behaviorConfiguration="jsonBehavior" binding="webHttpBinding" contract="Services.IMyService" bindingConfiguration=""/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
 </service>
...

Finally, the interface and implementation of MyService:最后是MyService的接口和实现:

[ServiceContract(Name = "MyService", Namespace = "http://myservice.com/services/2010/")]
public interface IMyService
{
    [OperationContract,
    WebInvoke(Method = "POST",
              ResponseFormat = WebMessageFormat.Json,
              BodyStyle = WebMessageBodyStyle.WrappedRequest,
              UriTemplate = "addentrant")]
    void AddEntrant(string firstName);
}
...
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyervice
{
...
    public void AddEntrant(string firstName)
    {
        Entrant entrant = new Entrant()
        {
            FirstName = firstName,
        };
        context.Entrants.InsertOnSubmit(entrant);
        context.SubmitChanges();
    }
}

I think that's everything.我认为这就是一切。 Anyway, the $.ajax call returns a success, but the web service method was not being called (I had a breakpoint set).无论如何,$.ajax 调用返回成功,但没有调用 Web 服务方法(我设置了断点)。 I opened up Fiddler and found I was being given a 405: Method Not Allowed .我打开 Fiddler,发现我收到了 405: Method Not Allowed I've seen that before, but only when I had forgotten to set up the method to allow POST requests.我以前见过,但只是在我忘记设置允许 POST 请求的方法时。 I'm very confused as to why it is doing this now.我很困惑为什么它现在这样做。

Also, oddly enough, if I clone the ajax request captured in Fiddler, I get the following:另外,奇怪的是,如果我克隆在 Fiddler 中捕获的 ajax 请求,我会得到以下信息:

OPTIONS /services/MyService.svc/addentrant HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost:6339
Access-Control-Request-Method: POST

Just the header, no request body to speak of.只是标题,没有请求正文可言。

What happens if you try to use GET instead of POST?如果您尝试使用 GET 而不是 POST 会发生什么?

Try clearing your cache or appending a timestamp to the url - the 200 response code may have been cached by the browser尝试清除缓存或将时间戳附加到 url - 200 响应代码可能已被浏览器缓存

Another thing to try is not setting the contentType in your $.ajax call and use dataType: "json" instead.另一件要尝试的事情是不要在$.ajax调用中设置contentType而是使用dataType: "json"

$.ajax({
    type: "POST",
    url: url,
    dataType: "json",
    data: stuff,
    timeout: 10000,
    success: function (obj) { alert('yay!'); }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM