Service call from Html

I want to call the asp.net web service from a java script and pass parameters to it. Is there any code sample or demo that will help me achieve this? thanks in advance

+3
source share
4 answers

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

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

+1
source

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
        }
    });
}
+2
source

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.
}
+2

, 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.

See links for link: 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

+2
source

Source: https://habr.com/ru/post/1777630/


All Articles