1

How to call Wcf Service from javascript in vb.net ?I am getting error "Service call failed: 415 Unsupported Media Type".

My WebService code:

<ServiceContract()> 
Public Class Service <OperationContract()> _
<WebInvoke(Method:="POST", 
BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Json)> _ 
Public Function MyFunction(ByVal Count As String) As String
Return String.Format("Welcome in WCF call")
End Function 
End Class

My HTML Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!DOCTYPE html>
    <title>Call WCF</title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
      <script type="text/javascript">
        var counter = 0;
        function CallMyService() {
            // debugger;
            //alert("hi");
            counter++;

            $.ajax({
                type: "POST",
                url: "http://localhost:25057/WCFVB/Service.svc/MyFunction",
                data: '{"Count": "' + counter + '"}',
                processData: false,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
//                contentType: "application/json", // content type sent to server
                success: ServiceSucceeded,
                error: ServiceFailed
            });
        }

        // ---- WCF Service call backs -------------------

        function ServiceFailed(result) {
            //debugger;
            Log('Service call failed: ' + result.status + '  ' + result.statusText);
        }

        function ServiceSucceeded(result) {
            // debugger;
            var resultObject = result.MyFunctionResult;
            Log("Success: " + resultObject);
        }


        function Log(msg) {
            $("#logdiv").append(msg + "<br />");
        }
    </script>

</head>
<body>
    <input id="Button1" type="button" value="Execute" onclick="CallMyService();" />

    <div id="logdiv"></div> 
</body>
</html>
12
  • Yes.I am going to write here. Commented Jun 11, 2013 at 8:13
  • ok, it's getting clearer... how does your endpoint config looks like? Have you tested it with a GET-Function? Does the Webservice works by calling a GET over url? Service and html-site are both on the same server? The HTML is not running local? Commented Jun 11, 2013 at 11:49
  • While using GET function its showing "Service call failed: 400 Bad Request" Commented Jun 11, 2013 at 12:06
  • do you run your html on the same server like the service? Commented Jun 11, 2013 at 12:17
  • yes Service and html-site are both on the same server but diff URL.Is my code is right or not? Commented Jun 11, 2013 at 12:21

1 Answer 1

1

Here's a sample of a little JavaScript request that runs against our WCF service - it's just a simple ping function in this case, but all the parts are there:

_________ edit _____________

 <html> 
 <head>     
 <meta http-equiv="Content-Type" content="text/xml; charset=UTF-8" /> 
 <title>SOAP JavaScript Client Test</title>     
 <script type="text/javascript">

 function Ping() {             
   //set up varable
   var sContent;
    // 'Content-Type: text/xml \r\n ' +
    // 'Host: localhost:8085 \r\n \r\n  ';
    var s;
    s+="<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    s+="<s:Body><HostConnect xmlns=\"http://SomeURL \">";
    s+="<inMsg xmlns:a=\"http://schemas.datacontract.org/2004/07/ \"      xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    s+="<a:TextSection>" ;
s+="&lt;Method value=\"ping\" &gt;";
s+="</a:TextSection>";
s+="</inMsg>";
s+="</HostConnect>";
s+="</s:Body>";
s+="</s:Envelope>";


   var xmlhttp =  new XMLHttpRequest();
   xmlhttp.open('POST', Demo.URL.value, true);  
   //xmlhttp.overrideMimeType("text/xml; charset=UTF-8"); /* ... */ 

 // alert(Demo.URL.value);
   xmlhttp.onreadystatechange = function() {
     if (xmlhttp.readyState == 4||xmlhttp.readyState == 0) {
        //alert("Ready state: " + xmlhttp.readyState.toString());
        if (xmlhttp.status == 200) {
        //alert("good");
        Demo.pingresponse.value = "Response: " +xmlhttp.responseText;
    }
    if (xmlhttp.status !=200){
        //alert("bad");
        Demo.pingresponse.value = "Error: " +xmlhttp.status.toString() +"  response text:  " +xmlhttp.responseText;
    }
     } else {
       //alert("readystate bad");
     }
}
           xmlhttp.setRequestHeader("POST http:servername:8085/HostInterface/HostConnect HTTP/1.1"); 
         xmlhttp.setRequestHeader("VsDebuggerCausalityData","uIAA"); 
     xmlhttp.setRequestHeader("SOAPAction","\"http://ServerName\""); 
     xmlhttp.setRequestHeader("Host","localhost:8085"); 
     xmlhttp.setRequestHeader("Expect","100-continue"); 
     xmlhttp.setRequestHeader("Accept-Encoding","gzip, deflate"); 
     xmlhttp.setRequestHeader("Connection","Keep-Alive"); 
     xmlhttp.setRequestHeader("Content-Length","639");                   
     xmlhttp.setRequestHeader("Content-type", "text/xml; charset=utf-8"); 
     xmlhttp.send(sContent);                  
  } 
 </script> 
  </head> 
  <body>     
  <form name="Demo" action="" method="post">         
  <div>
  Web Service URL 
  <input id="URL" type="text" size="140" value="" /><br />             
  <input type="button" value="Ping" onclick="Ping();" /><br />
  <textarea id="pingresponse"cols="100" rows="10"></textarea> <br />     
   </div>     
  </form> 
  </body> 
  <html> 

Try this:

Sign up to request clarification or add additional context in comments.

3 Comments

is it calling from HTML page or aspx ?I want calling from HTML page.
This is calling from an HTML page. I have a click event on a "Ping" button that executes this script.
What is the reference u have added to call this web service from HTML page?Can u post the Html page code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.