如何从 vb.net 中的 javascript 调用 Wcf 服务?我收到错误“服务调用失败:415 不支持的媒体类型”。
我的网络服务代码:
<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>