jquery cross-domain request, cross-domain post submission data method (. Net/sql Technology group 206656202 into the group need to indicate the blog Park)-think ...

Source: Internet
Author: User
Tags serialization tojson

jquery cross-domain request, cross-domain post submission data method (. Net/sql Technology group 206656202 into the group need to indicate the blog Park)-think ...

Boring, online look at the technical articles absorbed under the essence, inadvertently found a lot of developers in cross-domain request is very confused, I collated a once written code for the hard-to-find solution to the IT person a little inspiration, if you think you are a master of it, then you can float over the ~ ~ Nonsense not to avoid annoying ~ ~

First, get way to implement cross-domain request

Here I use the Jquery.getjson () function to implement
A site http://bj.xxxx.com.aspx request B site http://sjz.xxxx.com.aspx, demo code send user name password to b site Request Login
Send data:

Html><Head><title>getjson cross-domain request </title> </head><body><script type= "Text/javascript"  Src= "/js/jquery-1.6.4.min.js" ></ script><script type=< Span class= "value" > "Text/javascript" > $ (function () {var _username =  "user name"; var _pwd =  "password"; /*              
Jsoncallback cannot write to the JSON data being sent, otherwise there is a possibility that the server will not accept the data the request is actually sent out http://sjz.xxxx.com.aspx?action=checklogin &username=qsak47&password=123456&jsoncallback=jquery16406769416032866459_1393165804461&_= 1393165804718
*/
$.getjson ( "Http://sjz.xxxx.com.aspx?action=CheckLogin &jsoncallback=? ", {" password ": _pwd},
     
      function (data) {
      //callback function to perform a series of operations on the return value Alet (data.msg); }); }); </script></ body></html> the server receives data processing requests (how to receive or how to receive): 
     
String Username=request.querystring.get ("username");   String Password=request.querystring.get ("password");   String jsoncallback = Request.QueryString.Get ("Jsoncallback");  OK so we get the value at the B site, here is the simulation is login, if B site want to tell a site OK authentication through you can log in, then you need to return the data to a site, we should do this   I am accustomed to use a class to encapsulate the return value        jsonback 

{
<summary>
Whether the request was processed successfully
</summary>
public int code {get; set;}
<summary>
Tips
</summary>
public string msg {get; set;}
<summary>
Serialization of data
</summary>
Public object data {get; set;}
}

<summary>
Serialization of
</summary>
<param name= "obj" > Data Objects </param>
<returns> Serialization Results </returns>
public static string ToJSON (Object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer ();
Return serializer. Serialize (obj);
}

Jsonback back = new jsonback{code=1,msg= "Login Successful", data=true};

String jsondate = ToJSON (back);

Response.Write (Jsoncallback + "(" + Jsondate + ")");

Response.End ();

Ok! Jqqury.getjson end!!!!

Second, post mode

Because the Getjson method is a GET request, the parameters are passed through the URL, for a small amount of data transmission is not a problem, the HTTP protocol for the URL length is not limited, but the browser manufacturer has limited the specific maximum length is how many browsers are different, There's a lot of information about this, and I'm not going to talk about it, we can use Firebug to test it, and there's a 404 error when the URL is too long.

There is such a situation, a site of one of the pages need to submit a large amount of data to B site, but can not modify the site a page HTML content can only be implemented with JS, but the data still have to submit ah, it doesn't matter I put on the solution, the premise is a Site page reference a JS file: a.js

Post submit data PARAMS as JSON format parameter
Function post (URL, PARAMS) {
var temp = document.createelement ("form");
Temp.action = URL;
Temp.method = "POST";
Temp.style.display = "None";
for (var x in PARAMS) {
var opt = document.createelement ("textarea");
Opt.name = x;
Opt.value = Params[x];
Temp.appendchild (opt);
}
Document.body.appendChild (temp);
Temp.submit ();
return temp;
}

The principle is very simple, JS dynamically created a hidden form form, specify the URL of the submission, the request method is post, how many parameters to create the number of textarea, set the Name property for each textarea, the value is the name of the parameter, the value of textarea is the value of the parameter, Add textarea to form form, add form form to body, OK!!!!!

Next

var data = "Large amount of 1";

var comment= "Large amount of data 2";

var params = {"Data":d ata, "comment": comment};

Call Post submit data

Post ("http://sjz.xxxx.com", params);

End!!!

Hope to be helpful to everyone

jquery cross-domain request, cross-domain post submission data method (. Net/sql Technology group 206656202 into the group need to indicate the blog Park)-think ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.