"Service Side"
[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=wsiprofiles.none)] [ToolboxItem (false)] [System.Web.Script.Services.ScriptService] Public classService1:System.Web.Services.WebService {[WebMethod] Public voidHelloWorld (stringjsonstudent) {HttpResponse resp=Context.response; stringCallBack = context.request["Callback"]. ToString (); Resp. Clear (); Resp. ContentEncoding=NewUTF8Encoding (false); Resp. ContentType="Application/json"; Resp. Charset="Utf-8"; Student Stu= jsonserializer<student>. Deserialize (jsonstudent); Resp. Write (CallBack+"("+jsonserializer<student>. Serialize (Stu) +")"); Resp. End (); } [WebMethod] Public voidHello (stringJS) {HttpResponse resp=Context.response; Resp. Clear (); Resp. Charset="Utf-8"; stringCallBack = context.request["Callback"]. ToString (); Resp. ContentEncoding=NewUTF8Encoding (false); Resp. ContentType="Application/json"; stringCallbackfun = CallBack +"('"+ JS +"')"; Resp. Write (Callbackfun); Resp. End (); } }
Client
First, complete the wording:
$.ajax ( { URL:"Http://localhost:3997/Service1.asmx/HelloWorld", data:" Jsonstudent={\ "id\": \ "1\", \ "name\": \ "promotional films featuring \"} ", //preferably according to the standard notation contentType:" Application/json ", type:"POST", dataType:"Jsonp", success:function( Data) { alert (data.id+ "<===>" +data.name); } });
Second, simplify the wording:
$.getjson ("http://localhost:3997/Service1.asmx/Hello?callback=?", {"JS": "Promotional films featuring"}, function (data) { alert (data); },"post");
Cross-domain invocation of javascript