Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <asp:ScriptManager ID="ScriptManager" runat="server">
- <Services>
- <asp:ServiceReference Path="~MyService.asmx" />
- </Services>
- </asp:ScriptManager>
- $.ajax({
- type: "POST",
- url: "~/MyService.asmx/MyMethod",
- data: "{parameterName:'" aStringArgument + "'}",
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function(msg) {
- var data = msg.d
- // Now var is an object with properties just like your object
- }
- });
- [WebMethod]
- [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
- public String Foo(String p1, String p2)
- {
- return "Hello World";
- }
- var httpobj = getXmlHttpRequestObject();
- //Gets the browser specific XmlHttpRequest Object
- function getXmlHttpRequestObject()
- {
- if (window.XMLHttpRequest)
- return new XMLHttpRequest();
- else if(window.ActiveXObject)
- return new ActiveXObject("Microsoft.XMLHTTP");
- }
- CallService()
- {
- //Set the JSON formatted input params
- var param = "{'p1' : 'value1', 'p2' : 'value2'}";
- //Send it to webservice
- if(httpobj.readyState == 4 || httpobj.readyState == 0)
- {
- httpobj.open("POST", 'service.asmx/' + 'Foo', true);
- //Mark the request as JSON and UTF-8
- httpobj.setRequestHeader('Content-Type','application/json; charset=utf-8');
- httpobj.onreadystatechange = OnSuccess;
- httpobj.send(param);
- }
- }
- //Called on successfull webservice calls
- OnSuccess()
- {
- if (httpobj.readyState == 4)
- {
- //Retrieve the JSON return param
- var response = eval("(" + httpobj.responseText + ")");
- }
- }
Add Comment
Please, Sign In to add comment