Android 在Visual studio 2015中调用cordova中的web服务
我正在visual studio 2015中使用apache cordova工具开发android应用程序。我想在cordova应用程序中从我的索引页调用web服务,但不知何故我无法实现 这是HTMLAndroid 在Visual studio 2015中调用cordova中的web服务,android,web-services,cordova,visual-studio-2015,apache-cordova,Android,Web Services,Cordova,Visual Studio 2015,Apache Cordova,我正在visual studio 2015中使用apache cordova工具开发android应用程序。我想在cordova应用程序中从我的索引页调用web服务,但不知何故我无法实现 这是HTML <div ><input type="button" id="callwebmethod" name="submit" /> <br /> </div> 您的var参数必须与WebMethod的参数相同。让它们空着,然后再试一次。它们必须完全相同
<div ><input type="button" id="callwebmethod" name="submit" /> <br /> </div>
您的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";
}
这个例子对我来说很有用:
var person = {};
person.ID = $('#txtID').val();
person.Name = "Amir";
var pdata = { "p": person };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/SampleService.asmx/GetPesonnelSalary",
data: JSON.stringify(pdata),
dataType: "json",
async: true,
success: function (data, textStatus) {
if (textStatus == "success") {
if (data.hasOwnProperty('d')) {
msg = data.d;
} else {
msg = data;
}
alert(msg);
}
},
error: function (data, status, error) {
alert("error");
}
});
完整的代码如下:thanx@deru,感谢您花时间回答我的问题。然而,我发现代码中的问题是,我需要启用CORS。我在我的服务器web配置中添加了http协议并允许访问origin=“*”,这就成功了。
data: "{}"
[WebMethod]
public string App_Test() {
return "Hello";
}
var person = {};
person.ID = $('#txtID').val();
person.Name = "Amir";
var pdata = { "p": person };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/SampleService.asmx/GetPesonnelSalary",
data: JSON.stringify(pdata),
dataType: "json",
async: true,
success: function (data, textStatus) {
if (textStatus == "success") {
if (data.hasOwnProperty('d')) {
msg = data.d;
} else {
msg = data;
}
alert(msg);
}
},
error: function (data, status, error) {
alert("error");
}
});