none
ajax wcf post soap data to another server RRS feed

  • Question

  • I can't get an ajax call to another server to work. It works local and it works if I do it with c# code or using a program like soapui so I suppose my browser has something to do with it. I have tried in chrome, ie and firefox. The wsdata below contains a soap envelop. I get the message "200 ok" but the response contains what looks like the page that is shown if you paste "http://....servicev.svc" in your browser.

    I'm using.Net 3.5.
    I have added lines to web.config.

    <add name="Access-Control-Allow-Origin" value="*"/>
    <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS"/>
    <add name="Access-Control-Allow-Headers" value=""/>
    <add name="Access-Control-Max-Age" value="1728000"/>

    I have wshttpbinding and basichttpbinding.

    function invokeService() {
        $(document).ready(function() {                      
          $.ajax({
              type: "POST",
              //async: "false",
              url: "http://....servicev.svc",
              data: wsdata,
              contentType: "application/soap+xml; charset=utf-8",
              //dataType: "jsonp",
              //processData: true,
              success: function(result) {
                  AjaxSucceeded(result);
              },
              error: AjaxFailed
          });
      });
    }
    function AjaxSucceeded(result) {
        alert(result);
        result();
    }

    function AjaxFailed(result) {
        alert(result.status + ' ' + result.statusText);
    }

    Friday, March 21, 2014 8:57 AM

All replies

  • Hi,

    First please try to check that the port on the server isn't being blocked by the firewall.

    Then I wonder if you are calling Cross Domain WCF Service, then if so, please try to check these articles:
    #Calling Cross Domain WCF Service using Jquery:
    http://www.dotnet-tricks.com/Tutorial/wcf/X8QN260412-Calling-Cross-Domain-WCF-Service-using-Jquery.html.
    #Cross Domain WCF service Call using AJAX:
    http://mrsarker.wordpress.com/2012/02/23/cross-domain-wcf-service-call-using-ajax-ajax-call-to-https-service-from-http-page/ .

    Best Regards,
    Amy Peng



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 24, 2014 2:58 AM
    Moderator
  • Thanks.

    The service runs on port 80 and is not blocked by the firewall. 

    I can't use crossDomainScriptAccessEnabled since the service is in .Net 3.5.

    Is there a way using javascript to call a wcf service (.Net 3.5) from another server?

    Monday, March 24, 2014 8:52 AM
  • Hi,

    Please try to check the following example:
    WCF Service:

     public class Service1 : IService1 {
    
          public string GetData(int value) {
              return string.Format("You entered: {0}", value);
          }
      }
    
    


    Javascript:

    function Retrieve(id) {
     jQuery.support.cors = true;
     var soapData = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' + '<s:Body><GetData xmlns="http://tempuri.org/"><value>200</value></GetData></s:Body></s:Envelope>';
    
    
     $.ajax({
    
         type: "POST",
         contentType: "text/xml; charset=utf-8",
         dataType: "xml",
         url: "http://localhost:58282/Service1.svc",
         data: soapData,
         beforeSend: function (xhr) {
           xhr.setRequestHeader("SOAPAction", "http://tempuri.org/IService1/GetData");
         },
         success: RetrieveCallbackSuccess,
         error: RetrieveCallbackError
     });}
    
    function RetrieveCallbackSuccess(response) {     alert("Yey! :) --> " + response.text); }
    
    function RetrieveCallbackError(err) {     alert("Onoz! :( --> " + err.statusText); }
    
    
    

    The line "jQuery.support.cors = true" is important for cross domain requests.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 1, 2014 2:57 AM
    Moderator
  • Thank you.

    I have tried with your code but when changing from localhost to another url I'm still stuck with "Invalid HTTP status code 400".

    Thursday, April 3, 2014 9:44 AM