jquery cross-domain requests and. NET background cross-domain requests

Source: Internet
Author: User
Tags ticket

The season for the annual train ticket has come again, as a programmer, in the study of the interfaces provided by 12306, encountered some cross-domain issues. Take the query for the remainder of the ticket interface as an example.
The first method:
$.ajax ({
URL: ' Https://kyfw.12306.cn/otn/leftTicket/queryT?leftTicketDTO.train_date=2015-02-05&leftTicketDTO.from_ Station=szh&leftticketdto.to_station=shh&purpose_codes=adult ',
Data: [],
DataType: ' Jsonp ',
Type: ' GET ',
Cache:false,
Async:false,
JSONP: "Callback",//pass to the request handler or page, to obtain the name of the JSONP callback function name (default: Callback)
Jsonpcallback: "Success_jsonpcallback",
Error:function (e) {
Debugger;
$ ("#show"). empty ();
$ ("#show"). Append (e);
                },
success:function (data) {
Debugger;
$ ("#show"). empty ();
$ ("#show"). Append (data);
                }
            });
function Success_jsonpcallback (backdata) {


the requirement for data to be requested across domains in the JSONP format is that the client and server side should have a contractual format. Because we do not know the agreed format of 12306, there is a scene when we request data in this way.

Because the name of the callback function is overridden in a JSONP request. This value is used instead of "callback=?" This get or POST request in the URL parameter in the "Callback" section, because we do not know the name of the 12306 Convention callback parameter, so the URL syntax error.

The second method:

var data = {"Leftticketdto.train_date": "2015-02-05", "leftticketdto.from_station": "Szh", "            Leftticketdto.to_station ":" SHH "," purpose_codes ":" Adult "}; $.getjson ("https://kyfw.12306.cn/otn/leftTicket/queryT?callback=", data, function (backdata) {                 Debugger;                     //Actually the function returned in the background has been replaced with this function, so backdata   //is a JSON object that you've sent back from the background.                            alert (backdata); }); the $.getjson () method is actually the simple version of the first method, which supports cross-domain. Third method:. NET background
String URL = "Https://kyfw.12306.cn/otn/leftTicket/queryT";

/* cross-domain request the first method */
byte[] param = System.Text.Encoding.ASCII.GetBytes ("Leftticketdto.train_date=2015-02-05&leftticketdto.from_ Station=szh&leftticketdto.to_station=shh&purpose_codes=adult ");
HttpWebRequest req = (HttpWebRequest) httpwebrequest.create (URL);
Req. Method = "POST";
Req. Contenttype= "application/x-www-form-urlencoded";
Req. ContentLength = param. Length;
Stream Reqstream = req. GetRequestStream ();
Reqstream.write (Param,0,param. Length);
Reqstream.close ();
HttpWebResponse response = (HttpWebResponse) req. GetResponse ();
StreamReader reader = new StreamReader (response. GetResponseStream (), encoding.getencoding ("GB2312"));
String content = Reader. ReadToEnd ();
Reader. Close ();
Response. Close ();
The Fourth method:System.Net.WebClien T wcient = new System.Net.WebClient (); wCient.Headers.Add ("Content-type", "application/x-www-form-urlencoded"); byte[] PostData = System.Text.Encoding.ASCII.GetBytes ("id=" + Urlargs);byte[] ResponseData = Wcient.uploaddata (URL, "POST", postdata);string returnstr = System.Text.Encoding.UTF8.GetString (responsedata);//Returns the accepted datacontext. Response.ContentType = "Text/plain"; Context.  Response.Write (RETURNSTR);

jquery cross-domain requests and. NET background cross-domain requests

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.