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

标签 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>

这是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/

相关文章:

plugins - Cordova 3.0.0 - 如何安装插件

ios - 错误 : Module org. apache.cordova.console 不存在

android - 服务器发送的事件不能通过使用域名 URL 在 android 中工作

c# - Android 与 PC 的连接

android - android中的WiFi编程

java - 升级到 Android 4.2.2 后出现奇怪错误 - Activity 已泄漏 IntentReceiver

javascript - 如何在网站中使用天气 API

java - 将 Web 服务与 javax.ws 或 javax.jws 结合使用

java - OpenCV 2.4 双边滤波

javascript - 我应该选择哪个 Google map 选项?