如何使用vb.net代码从javascript调用Wcf服务?

时间:2013-06-11 07:53:25

标签: javascript vb.net wcf

如何从vb.net中的javascript调用Wcf服务?我收到错误“服务调用失败:415不支持的媒体类型”。

我的WebService代码:

<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

我的HTML代码:

<!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>

1 个答案:

答案 0 :(得分:1)

这是针对我们的WCF服务运行的一个小JavaScript请求的示例 - 在这种情况下它只是一个简单的ping函数,但所有部分都在那里:

__ _ __ _ ___ 编辑 __ _ __ _ __ _ __ _ _

 <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> 

试试这个: