Ajax cross-origin access to webservice and ajax cross-origin webservice
Front-end code
$.ajax({ type: "POST", url: "http://localhost:9767/WebService1.asmx/HelloWorld?jsoncallback=?", data: "{}", dataType: "jsonp", success: function (data) { alert(data.result); } });
Webservice code
public void HelloWorld() { string callbackMethodName = HttpContext.Current.Request.Params["jsoncallback"] ?? ""; HttpContext.Current.Response.Write(callbackMethodName + "({result:'Hello World'})"); HttpContext.Current.Response.End(); }
This will get
However, if such code is used
If the url is not added? Jsoncallback =?
$(function () { $.ajax({ type: "POST", url: "http://localhost:9767/WebService1.asmx/HelloWorld", data: "{}", dataType: "jsonp", success: function (data) { alert(data.result); } });
public string HelloWorld() { return "Hello World"; }
Then, an xml (
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
) And I will not go back to the callback function. I don't know much about webservice, and I have just been familiar with it. I would like to express my gratitude to you.