WCF Web Service returns “Bad Request” error when invoked through Javascript

后端 未结 3 1765
悲哀的现实
悲哀的现实 2021-01-16 17:11

I am trying to make an ajax GET request to a WCF web service method through javascript. The request returns with a 400 \"Bad Request\" error everytime. But if I invoke the s

3条回答
  •  萌比男神i
    2021-01-16 17:51

    I see that the biggest difference between the WCF test client request and my ajax request is that I make a GET request and pass in the name of the web service method in the url while the WCF test client makes a post request and sends in the web service method name in a SOAP envelope.

    Seems that your service is configured with SOAP binding and cannot work with AJAX Get requests.

    You need to use WebHttpBinding if you want to consume your service from JS.

    1. Add [WebGet] attribute got your operation
    2. Change binding in your endpoint - binding="webHttpBinding"
    3. Configure behavior for endpoint

    There are many samples in internet. For example this one http://bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/

    
      
        
      
    
    

    You need to add service section to your web.config file. Host does not know that you want to use webHttpBinding unless you tell him.

    
      
        
      
    
    

    Link below provides detailed instructions for hosting service in IIS (with wsHttpBinding). You just need to use webHttpBinding instead of wsHttpBinding - http://msdn.microsoft.com/en-us/library/ms733766.aspx

提交回复
热议问题