About Ajax cross-domain

Source: Internet
Author: User

I wrote a test page because of my job requirement, and then I went to a site to request data after filling out the information, and then returned the results! The first is to use Ajax directly in the script to access, no big deal (because the target address is a site on this computer), but when the site to the external site, found that the results are not right! So asked the degree Niang, get the result is Ajax cross-domain problem! On this issue is not much to say, here to a link:

After reading felt good, but the background is written in Java! And I was learning. NET!

There are several issues: 1. Although cross-domain issues are resolved, the requested file format is limited, JSONP

2. Background code needs to be modified (but the test site is not I wrote, not I said change can change)

Then another solution came out: Use intermediate media!

Method: Ajax---->ASHX------> Destination Address

Explanation: It is Ajax writing to itself a ashx to request, and then in the Ashx C # code to the destination address to send a request, and then return the results

So it leads to two things called: HttpWebRequest and HttpWebResponse.

1:get Way Request

HttpWebRequest req = (HttpWebRequest) httpwebrequest.create ("http://fanyi.baidu.com/transcontent");//Create Request object, If you have parameters, write them in the URL.

  

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

{

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

{

ResponseData = reader. ReadToEnd (). ToString ();//This is the result, as to how to deal with it, see for yourself

}

}

is actually to use HttpWebRequest to create a request object, and then get the response stream, read the data, if there are parameters directly written in the URL with the past (do not ask me how the URL with parameters)

2:post Way 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 ";//And get different is the argument string to separate out

byte[] bs = Encoding.ASCII.GetBytes (param);//Change the argument string to a byte array

string responsedata = String.Empty;

Req. method = "POST";//Set submission method, default is Get

Req. ContentType = "application/x-www-form-urlencoded";//SET HEADER type!!!!! is important

Req. ContentLength = BS. length;//Length of byte array

using (Stream Reqstream = req. GetRequestStream ())

{

Reqstream.write (BS, 0, BS. LENGTH);//byte array to write the parameter string to the request Stream!!!! How to submit parameters by post

Reqstream.close ();

}

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

{

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

{

ResponseData = reader. ReadToEnd (). ToString ();//Gets the result, which can be handled separately according to the data type of the response

}

}

The interpretation is: Set up the Request object, set the submission method, set the header, get the request stream, write a byte array of the parameter string, and then get the response stream

About Ajax cross-domain

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.