Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

How to call Webservices through an API using Jquery from an MVC application



Thanks & Regards,
Soumya
Posted

Here I have solved with the below coding


function callservice() {


var pageUrl = "../webservices/WebService1.asmx"


$.ajax({
type: "POST",
url: pageUrl + "/HelloWorld",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});


}


function OnSuccessCall(response) {
alert(response.d);

}


function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}



XML
<body>
<form id="fm1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="callservice();"/>
    <div id="divresponse">

    </div>
    </form>
</body>
</html
 
Share this answer
 
I have added Below lines of coding .but it display a messagebox with a message 'undefined'


XML
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript" language="javascript">

        function callservice() {

            $.ajax({

                type: "POST",
                url: "http://www.webservicex.net/country.asmx?wsdl",
                //        data:'{}',
                contenttype: "application/json;charset=utf-8",
                datatype: "json",
                success: onsuccesscall (response),
                Error: onerrorcall
            });
        }
function onsuccesscall(response)
{
alert(response.d);
    $("#divresponse").text(response.d);
//   alert('hello');
}

function onerrorcall() {
    alert('not working');

}
    </script>
</head>
<body>
<form id="fm1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="callservice();"/>
    <div id="divresponse">

    </div>
    </form>
</body>
</html>


------------------------------------------------------------
In HomeController I have added below lines of coding
SQL
public ActionResult web_ser_call()
       {
           return View();

       }
 
Share this answer
 
Comments
Member 9441893 26-Oct-12 7:21am    
what is response
Well, its not that much related to MVC, if you want to use JQuery to call your web service you can use the $.ajax method (tons of examples and links on using that), keep in mind that will call the web service from the client side and it has nothing to do whether you are using MVC or ASP.NET forms or whatever it is your web application.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900