Two days ago, a friend asked me to help him write a program: in ASP. asp page, after the data submitted to the other side of the database, according to the return value (return value: OK or error), if OK and then fill in the local database. At that time, take for granted, feel very simple, with JS XMLHTTP, if the value according to response is "OK" on the execution of the local database. Soon write the past, let friends try, a try to find no, then a ask, the original is cross-domain access, I ignored, so let a friend to the ASP into a Web service, friends said the program is a cooperative company to do, only ASP, will not use the Web service, crazy dizzy ing. No way, can only ask out the WebResponse, a lot of Web site collection procedures are used this. The first version of the finished, but can cross-domain access, but garbled, adjust the way of coding, finally can. This application is small but involves a number of knowledge points:
1. XMLHTTP cannot be submitted across domains.
Of course, XMLHttpRequest is the expedient solution,
2, WebResponse can be cross-domain access, but pay attention to
1), get and post differences.
2), pay attention to the problem of timeout.
These are simple procedures, note the memo, the master will not have to read.
No more nonsense, here's the relevant C # code:
| The code is as follows |
Copy Code |
<summary> Send data using the Post method </summary> <param name= "Pi_strposturl" > Submit address </param> <param name= "Pi_strparm" > Parameters </param> <returns></returns> public static string Postresponse (String Pi_strposturl, String pi_strparm) { Try { Coding Encoding t_encoding = encoding.getencoding ("GB2312"); Uri T_uri = new Uri (Pi_strposturl); byte[] parambytes = t_encoding.getbytes (pi_strparm); WebRequest t_webrequest = WebRequest.Create (T_uri); T_webrequest.timeout = 100000; Set ContentType T_webrequest.contenttype = "application/x-www-form-urlencoded";
T_webrequest.method = EnumMethod.POST.ToString (); Initialization using (Stream T_restream = T_webrequest.getrequeststream ()) { Send data requestStream.Write (parambytes, 0 , parambytes.length); }
WebResponse t_webresponse = T_webrequest.getresponse ();
using (StreamReader T_streamreader = new StreamReader (t_webresponse. GetResponseStream (), t_encoding)) { return T_streamreader.readtoend (); } } Catch { return "ERROR"; } }
public static string GetResponse (String Pi_strposturl, String pi_strparm) { Try { Coding Encoding t_encoding = encoding.getencoding ("GB2312"); Uri T_uri = new Uri (string. Format ("{0}?{ 1} ", Pi_strposturl, Pi_strparm));
WebRequest t_webrequest = WebRequest.Create (T_uri);
T_webrequest.timeout = 100000; T_webrequest.contenttype = "application/x-www-form-urlencoded";
T_webrequest.method = EnumMethod.GET.ToString (); WebResponse t_webresponse = T_webrequest.getresponse (); using (StreamReader T_streamreader = new StreamReader (T_webresponse.getresponsestream (), t_encoding)) { return T_streamreader.readtoend (); } } catch (Exception e) { return e.tostring (); } } public static string Ationresponse (String pi_url, Enummethod Pi_method) { String T_strurlpath= ""; String t_parm = ""; Uri T_url = new Uri (Pi_url); T_parm= T_url.query; if (Parmstring.startswith ("?")) T_parm = T_parm. Remove (0, 1); T_strurlpath = "http://" + T_url. Authority + T_url. Absolutepath; Return GetResponse (T_strurlpath, T_parm, Pi_method); } public enum Enummethod { POST, GET } |
Now that jquery Ajax supports cross-domain, let's look at an example that we can do with JSON data.
Jquery.getjson also supports JSONP data-mode invocation.
Example of calling code for client Jquery.ajax:
| The code is as follows |
Copy Code |
$.ajax ({ Type: "Get", Async:false, URL: "Http://www.xxx.com/ajax.do", DataType: "Jsonp", JSONP: "Callbackparam",//parameter for the server to receive the function name of the callback call Jsonpcallback: "Success_jsonpcallback",//callback function name Success:function (JSON) { alert (JSON); alert (json[0].name); }, Error:function () { Alert (' fail '); } }); |
Sample code that returns data from the server:
| The code is as follows |
Copy Code |
public void ProcessRequest (HttpContext context) { Context. Response.ContentType = "Text/plain"; String Callbackfunname = context. request["Callbackparam"]; Context. Response.Write (Callbackfunname + "([{Name:" John "}])"); } |
The Jquery.getscript method is similar to the principle, also need to support the service side return data, the difference is that the service side returns different results. Instead of returning a callback function call, the result is assigned directly to the variable name passed by the request. The client then loads the returned data source as if it were introduced as an external script: http://www.111cn.net/net/net/56393.htm.
WebResponse cross-Domain access example in ASP.