Problem sending JSON data from JQuery to WCF REST method

后端 未结 5 727
小蘑菇
小蘑菇 2020-11-30 01:27

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

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

         


        
5条回答
  •  执念已碎
    2020-11-30 02:00

    I'll just post a short answer that helped me, because other answers didn't.

    • Scenario: ajax call to wcf service.
    • Error cause: automatic OPTIONS request from ajax before sending POST request. The first request could not be handled by my service.
    • Solution: allow OPTIONS request, and respond to it.

    What you need to do:

    1. Add this to web.config:

      
      
        
          
          
          
        
      
      

    2. Add this to Global.asax.cs (if you don't have this file in your solution, then create it by: Add new item => Visual C# => Global Application Class (default name is "Global.asax")):

      protected void Application_BeginRequest(object sender, EventArgs e)
      {
          if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
                          HttpContext.Current.Response.End();
      }
      

提交回复
热议问题