Js cross domain access sample of of client and server
- 2020-03-30 03:02:31
- OfStack
<div id="oid"></div>
<script type="text/javascript">
//To get the article number
$.ajax({
url: "http://192.168.1.191/H.ashx",
type: "GET",
dataType: 'jsonp',
//The value of jsonp is custom, and if a jsoncallback is used, an object corresponding to the value of the jsoncallback is returned on the server side.
jsonp: 'jsoncallback',
//To pass the parameter, no pass parameters, must also be written
data: null,
timeout: 5000,
//Return Json type
contentType: "application/json;utf-8",
//The object returned by the server segment contains name,openid.
success: function (result) {
document.getElementById('oid').innerText=result.name+" : "+result.openid;
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
</script>
The service side H.a SHX
<%@ WebHandler Language="C#" Class="H" %>
using System;
using System.Web;
public class H : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string result = context.Request.QueryString["jsoncallback"] + "({"name":" Test number: ","openid":"123456789"})";
context.Response.Clear();
context.Response.Write(result);
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}