gpt4 book ai didi

android - 在 Visual Studio 2015 中调用 cordova 中的 Web 服务

转载 作者:行者123 更新时间:2023-11-29 01:22:57 24 4
gpt4 key购买 nike

我正在 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 的参数。将它们留空,然后再试一次。它们必须完全相同。

如果您想使用带有参数的网络方法,这里有一个工作示例:

    $.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com