如何使用 jQuery 调用 ASP.NET Web 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/230401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:48:58  来源:igfitidea点击:

How to use jQuery to call an ASP.NET web service?

asp.netjqueryweb-services

提问by Simara

I'm trying to use jQuery to get data from an ASP.NET web service (SharePoint Server 2007 lists.asmx), but any call to a web service will really help as a first step in that direction.

我正在尝试使用 jQuery 从 ASP.NET Web 服务(SharePoint Server 2007 列表.asmx)获取数据,但任何对 Web 服务的调用都将真正有助于朝着这个方向迈出的第一步。

回答by Bobby Borszich

I use this method as a wrapper so that I can send parameters. Also using the variables in the top of the method allows it to be minimized at a higher ratio and allows for some code reuse if making multiple similar calls.

我将此方法用作包装器,以便我可以发送参数。此外,使用方法顶部的变量可以以更高的比率将其最小化,并在进行多次类似调用时允许一些代码重用。

function InfoByDate(sDate, eDate){
    var divToBeWorkedOn = "#AjaxPlaceHolder";
    var webMethod = "http://MyWebService/Web.asmx/GetInfoByDates";
    var parameters = "{'sDate':'" + sDate + "','eDate':'" + eDate + "'}";

    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $(divToBeWorkedOn).html(msg.d);
        },
        error: function(e){
            $(divToBeWorkedOn).html("Unavailable");
        }
    });
}

I hope that helps.

我希望这有帮助。

Please note that this requires the 3.5 framework to expose JSON webmethods that can be consumed in this manner.

请注意,这需要 3.5 框架来公开可以以这种方式使用的 JSON webmethods。

回答by mohammedn

Here is an example to call your webservice using jQuery.get:

以下是使用 jQuery.get 调用您的网络服务的示例:

$.get("http://domain.com/webservice.asmx", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
  });

In the example above, we call "webservice.asmx", passing two parameters: name and time. Then, getting the service output in the call back function.

在上面的例子中,我们调用“webservice.asmx”,传递两个参数:名称和时间。然后,在回调函数中获取服务输出。

回答by Herb Caudill

I don't know about that specific SharePoint web service, but you can decorate a page method or a web service with <WebMethod()>(in VB.NET) to ensure that it serializes to JSON. You can probably just wrap the method that webservice.asmx uses internally, in your own web service.

我不知道那个特定的 SharePoint Web 服务,但是您可以使用<WebMethod()>(在 VB.NET 中)装饰页面方法或 Web 服务以确保它序列化为 JSON。您可以将 webservice.asmx 在内部使用的方法包装在您自己的 Web 服务中。

Dave Ward has a nice walkthroughon this.

Dave Ward在这方面有一个很好的演练

回答by Kaveh

$.ajax({
 type: 'POST',
 url: 'data.asmx/getText',
 data: {'argInput' : 'input arg(s)'},
 complete: function(xData, status) {
 $('#txt').html($(xData.responseXML).text()); // result
 }
});

回答by Vadim Gremyachev

SPServicesis a jQuery library which abstracts SharePoint's Web Services and makes them easier to use

SPServices是一个 jQuery 库,它抽象了 SharePoint 的 Web 服务并使其更易于使用

It is certifiedfor SharePoint 2007

它已通过SharePoint 2007认证

The list of supported operations for Lists.asmx could be found here

可以在此处找到 Lists.asmx 支持的操作列表

Example

例子

In this example, we're grabbing all of the items in the Announcements list and displaying the Titles in a bulleted list in the tasksUL div:

在这个例子中,我们抓取了Announcements 列表中的所有项目,并在tasksUL div 的项目符号列表中显示了Titles:

<script type="text/javascript" src="filelink/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="filelink/jquery.SPServices-0.6.2.min.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Announcements",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>
<ul id="tasksUL"/>

回答by Brandon Joyce

I have a decent example in jQuery AJAX and ASMXon using the jQuery AJAX call with asmx web services...

我在jQuery AJAX 和 ASMX 中有一个很好的例子,关于将 jQuery AJAX 调用与 asmx Web 服务一起使用...

There is a line of code to uncommment in order to have it return JSON.

有一行代码需要取消注释才能返回 JSON。

回答by Brandon Joyce

I quite often use ajaxpro along with jQuery. ajaxpro lets me call .NET functions from JavaScript and I use jQuery for the rest.

我经常将 ajaxpro 与 jQuery 一起使用。ajaxpro 允许我从 JavaScript 调用 .NET 函数,其余的我使用 jQuery。