从 Html 调用服务

发布于 2024-10-04 18:06:18 字数 70 浏览 0 评论 0原文

我想从java脚本调用asp.net web服务并将参数传递给它。是否有任何代码示例或演示可以帮助我实现这一目标? 提前致谢

i want to call asp.net web service from java script and pass the parameters to it .is there any code sample or demostration that will help me to acheive that??
thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

罗罗贝儿 2024-10-11 18:06:18

jQuery:

function AddLocation(ParentID) {
    $.ajax({
        type: "POST",
        url: "../server.asmx/Save",
        data: "{'ID':'0','ParentID':'" + ParentID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            var item = document.createElement('option');
            item.value = data.d.split("$")[0];
            item.text = name;
            //do stuff
        }
    });
}

JQuery:

function AddLocation(ParentID) {
    $.ajax({
        type: "POST",
        url: "../server.asmx/Save",
        data: "{'ID':'0','ParentID':'" + ParentID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            var item = document.createElement('option');
            item.value = data.d.split("$")[0];
            item.text = name;
            //do stuff
        }
    });
}
穿透光 2024-10-11 18:06:18

jQuery 支持这种行为。您可以使用 jQuery 进行 ajax 调用,如下所示。该方法有两个成功和失败的回调函数。

function loadData()
{
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: 'methodurl',
        success: methodSuccedded,
        error: methodFailure
    });
}

function methodSuccedded()
{
    //do your logic.
}

function methodFailure()
{
    //do your logic.
}

jQuery supports this behavior. you can use jQuery to do the ajax call as show below. this method has two call back functions for success and for failure.

function loadData()
{
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: 'methodurl',
        success: methodSuccedded,
        error: methodFailure
    });
}

function methodSuccedded()
{
    //do your logic.
}

function methodFailure()
{
    //do your logic.
}
み零 2024-10-11 18:06:18

您可以使用 AJAX 来执行此操作,并以 JSON 对象的形式从服务器获取响应。

    var xmlHttp = new ActiveXObject("Microsoft.XmlHttp");
    var url = "Service1.svc/ajaxEndpoint/";
    url = url + "Sum2Integers";
    var body = '{"n1":';
    body = body + document.getElementById("num1").value + ',"n2":';
    body = body + document.getElementById("num2").value + '}';

    // Send the HTTP request
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/json");
    xmlHttp.send(body);

    // Create result handler 
    xmlHttp.onreadystatechange= function X()
    {

         if(xmlHttp.readyState == 4)
         {
              result.innerText = xmlHttp.responseText;
         }
    }

获取 JSON 格式的响应将帮助您将其评估为对象,并且您可以通过 JavaScript 对其进行操作。

请参阅这些链接以供参考:
http://blogs.msdn.com/b/alikl/archive/2008/02/18/how-to-consume-wcf-using-ajax-without-asp-net.aspx

http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx

You can do so, using AJAX, and get the response from the server as an JSON object.

    var xmlHttp = new ActiveXObject("Microsoft.XmlHttp");
    var url = "Service1.svc/ajaxEndpoint/";
    url = url + "Sum2Integers";
    var body = '{"n1":';
    body = body + document.getElementById("num1").value + ',"n2":';
    body = body + document.getElementById("num2").value + '}';

    // Send the HTTP request
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/json");
    xmlHttp.send(body);

    // Create result handler 
    xmlHttp.onreadystatechange= function X()
    {

         if(xmlHttp.readyState == 4)
         {
              result.innerText = xmlHttp.responseText;
         }
    }

Getting the response as JSON would help you evualte it asn object and u can act on it through JavaScript.

See these links for reference:
http://blogs.msdn.com/b/alikl/archive/2008/02/18/how-to-consume-wcf-using-ajax-without-asp-net.aspx

http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx

生活了然无味 2024-10-11 18:06:18

根据我的经验,下面的链接是一个相当不错的方法。

http://encosia.com/ 2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

The below link is a pretty decent method from my experience.

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文