1

我正在Visual Studio 2015中开发一个使用apache cordova工具的android应用程序。我想从我的索引页面调用cordova应用程序中的Web服务,但我无法实现它。在Visual Studio中调用科尔多瓦的Web服务2015

下面是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> 

这里是Web方法

[WebMethod] 
    public static string GetEmployees() 
    { 
     return "Hello World"; 
    } 

回答

1

你VAR PARAMS必须simular到的参数WebMethod。只需将它们留空并再试一次。他们必须完全一样。

如果whant使用与此指标的影响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"; 
    } 

通过所示的误差函数也可以找出什么错误。

要做到这一点没有paremeters你只需要把它们留空。

data: "{}" 


    [WebMethod] 
    public string App_Test() { 
     return "Hello"; 
    } 
+0

抽出时间来回答我的问题感谢名单@deru。但是,我的代码中的问题是,我需要启用CORS。我在我的服务器网络配置中添加了http协议和allow-acess-origin =“*”,这些都可以实现。 –

0

这woked我这个例子:

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"); 
    } 
}); 

完整的代码是在这里:http://www.scriptiny.com/2012/12/calling-asmx-web-service-via-jquery-ajax-2/

相关问题