About Ajax cross-origin and Ajax cross-Origin

Source: Internet
Author: User

About Ajax cross-origin and Ajax cross-Origin

I wrote a test page for my work requirements. after entering the information on the page, I requested data from a site and then returned the result! In the beginning, Ajax was directly used to access the website in the script, which is no major problem (because the destination address is a website on the local machine). However, when the website goes to an external website, the results are not very good! So I asked du Niang, and the result was an Ajax cross-origin question! About this question is not much said, here to give a link:

I thought it was amazing after reading it. Unfortunately, it was written in java in the background! And I learned. NET!

There are several problems: 1. Although the cross-origin problem is solved, the requested file format is limited, jsonp

2. The background code needs to be modified (but I did not write the test site, and I could change it if I did not change it)

 

So another solution was introduced: using intermediate media!

Method: Ajax ----> ashx ------> target address

Explanation: Ajax sends a request to a self-written ashx, sends a request to the target address in the C # code of ashx, and then returns the result.

 

So two things are introduced: HttpWebRequest and HttpWebResponse.

1: GET request

HttpWebRequest req = (HttpWebRequest) HttpWebRequest. Create ("http://fanyi.baidu.com/transcontent"); // Create a request object, written in the url if there is a parameter

  

Using (HttpWebResponse response = (HttpWebResponse) req. GetResponse ())

{

Using (StreamReader reader = new StreamReader (response. GetResponseStream ()))

{

ResponseData = reader. ReadToEnd (). ToString (); // This is the result.

}

}

In fact, it is to use HttpWebRequest to create a request object, get the response stream, and read the data. If there are parameters, directly write them in the url (do not ask me how to include parameters in the url)

2: POST request

HttpWebRequest req = (HttpWebRequest) HttpWebRequest. Create ("http://fanyi.baidu.com/transcontent ");

Encoding encoding = Encoding. UTF8;

String param = "ie = UTF-8 & source = txt & query = hello & t = 1327829764203 & token = 8a7dcbacb3ed72cad9f3fb079809a127 & from = auto & to = auto "; // different from Get, the parameter string must be independent.

Byte [] bs = Encoding. ASCII. GetBytes (param); // convert the parameter string into a byte array

String responseData = String. Empty;

Req. Method = "POST"; // sets the submission Method. The default value is get.

Req. ContentType = "application/x-www-form-urlencoded"; // set the header type !!!!! Important

Req. ContentLength = bs. Length; // The Length of the byte array

Using (Stream reqStream = req. GetRequestStream ())

{

ReqStream. Write (bs, 0, bs. Length); // Write the byte array of the parameter string to the request stream !!!! Method for submitting parameters in POST Mode

ReqStream. Close ();

}

Using (HttpWebResponse response = (HttpWebResponse) req. GetResponse ())

{

Using (StreamReader reader = new StreamReader (response. GetResponseStream (), encoding ))

{

ResponseData = reader. ReadToEnd (). ToString (); // obtain the result. You can process it based on the data type of the response.

}

}

The explanation is: Create a request object, set the submission method, set the header, get the request stream, write the byte array of the parameter string, and then obtain the response stream.

Related Article

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.