我正在 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 params 必须与 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";
}
关于android - 在 Visual Studio 2015 中调用 cordova 中的 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35693992/