Nowadays, jsonp is very popular, and many people are writing very complicated statements. Now, a simple call is provided.
First, write a server and create TestHandler. ashx.
[Csharp]
Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "application/x-javascript ";
String callback = context. Request. QueryString ["callback"]. Trim ();
String name = context. Request. QueryString ["name"]. Trim ();
String str = callback + "([{\" SuitName \ ": \" "+ name +" \ "}]);";
Context. Response. Write (str );
}
Htm code:
[Html]
<Div>
<Input type = "button" id = "btn1" value = "Test Handler"/>
<Div id = "msg" style = "background-color: Red; width: 100px; height: 50px">
</Div>
<Script type = "text/javascript">
Function displayResult (data ){
$ ("# Msg"). text (data [0]. SuitName );
}
$ (Function (){
$ ("# Btn1"). click (function (){
Var url = "TestHandler. ashx? Name = majiang & callback = displayResult"
Url = "http://starajax.vancl.com/GetSuitsWithLikesBySuitIds? SuitIds = 9396493 & callback = displayResult"
$. GetScript (url );
});
});
</Script>
</Div>
It's easy enough.
From the column dz45693