我正在Visual Studio 2015中使用apache cordova工具开发一个android应用。我想从cordova应用中的索引页面调用Web服务,但以某种方式无法实现。
这是HTML
<div ><input type="button" id="callwebmethod" name="submit" /> <br /> </div>
这是JS功能
<script type="text/javascript">
$('#callwebmethod').click(function () {
var params = "{'msg':'From Client'}";
$.ajax({
type: "POST",
url: "http://mysite/index.aspx/GetEmployees",
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) { alert(result.d); }
});
})
</script>
这是网络方法
[WebMethod]
public static string GetEmployees()
{
return "Hello World";
}
您的var参数必须与WebMethod的参数相似。请将它们留空,然后重试。它们必须完全相同。
如果您想将Web方法与参数一起使用,请参见以下示例:
$.ajax({
url: "http://systemservice/systemservice.asmx/App_Test",
data: "{ par1: '" + xxx + "', par2: '" + xxx + "'}",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data.d) {
//Do something
}
},
error: function (xhr) {
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}
})
[WebMethod]
public string App_Test(string par1, string par2) {
return "Hello";
}
使用所示的错误功能,您还可以找出问题所在。
要在没有参数表的情况下做到这一点,您只需将其留空。
data: "{}"
[WebMethod]
public string App_Test() {
return "Hello";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句