代码之家  ›  专栏  ›  技术社区  ›  Dusda

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

  •  4
  • Dusda  · 技术社区  · 14 年前

    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!'); }
    });
    

    上面的代码向本地IIS7.5服务器上Sitefinity中托管的WCF服务发出请求。下面是相关的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>
    ...
    

    最后,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();
        }
    }
    

    我想这就是一切。无论如何,$.ajax调用返回一个成功的,但是没有调用web服务方法(我设置了一个断点)。 我打开小提琴手,发现给我的是405:方法不允许 . 我以前见过,但只是在我忘记设置允许POST请求的方法时。我很困惑为什么它现在这么做。

    另外,奇怪的是,如果我克隆了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
    

    只有头,没有请求体。

    2 回复  |  直到 14 年前
        1
  •  0
  •   Basic    14 年前

    如果尝试使用GET而不是POST会发生什么?

    尝试清除你的缓存或者在url上附加一个时间戳-200个响应代码可能已经被浏览器缓存了

        2
  •  0
  •   David Hoerster    14 年前

    contentType 在你的 $.ajax 调用和使用 dataType: "json"

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