Mixed Use of get and post in HTTP Programming

Source: Internet
Author: User
The problem comes from the features and restrictions of get and post. For GET requests, we can easily use window. opener to communicate with the parent page, However, according to the HTTP protocol In ie, the maximum URL length is 2083 bytes, and the length of data transmitted by get is 2048 bytes. For post requests, although there is no limit on the maximum length, it is not convenient to use window. opener to communicate with the parent page.
There are many ways to use JavaScript to automatically convert a GET request into a POST request. The main idea is to dynamically construct an IFRAME, then, assign the URL parameter value in the GET request to the input control, set the form action address, and call the submit method.
Someone in the garden has given a solution, http://www.cnblogs.com/ppchen/archive/2008/03/18/1109607.html
The record is as follows: Code
VaR Postnewwin =   Function (URL ){
VaR Urlarr = URL. Split ( " ? " );
VaR Posturl = Urlarr [ 0 ];
VaR Postdata = Urlarr [ 1 ];
VaR IFRAME = Document. getelementbyid ( " Postdata_iframe " );
If ( ! IFRAME ){
IFRAME = Document. createelement ( " IFRAME " );
IFRAME. ID =   " Postdata_iframe " ;
IFRAME. scr =   " About: blank " ;
IFRAME. frameborder =   " 0 " ;
IFRAME. style. Width =   " 0px " ;
IFRAME. style. Height =   " 0px " ;

VaR Form = Document. createelement ( " Form " );
Form. ID =   " Postdata_form " ;
Form. Method =   " Post " ;
Form.tar get =   " _ Blank " ;

Document. Body. appendchild (IFRAME );
Iframe.content+doc ument. Write ( " <Body> "   + Form. outerhtml +   " </Body> " );
}
Iframe.contentdomaindoc ument. getelementbyid ( " Postdata_form " ). Innerhtml =   " <Input name = 'postdata' id = 'postdata' type = 'text' value =' "   + Postdata +   " '/> " ;
Iframe.contentdomaindoc ument. getelementbyid ( " Postdata_form " ). Action = Posturl;
Iframe.contentdomaindoc ument. getelementbyid ( " Postdata_form " ). Submit ();
};

For example, a function is used to open a new page with a GET request.
Function opennewwindow ()
{
VaR url = http://www.website.com/page1.aspx? P1 = V1 & p2 = V2;
VaR win = Window. Open (URL );
}
Because of the length of Ur, we can use post to submit the request as follows:
Function opennewwindow ()
{
VaR url = http://www.website.com/page1.aspx? P1 = V1 & p2 = V2;
// Var win = Window. Open (URL );
Postnewwin (URL );
}

One problem here is that after using this method, you cannot use window. opener to interact with the parent page, because through this function, the GET request has been changed to a POST request. In order to use the post method to increase big data, you can also use window. open (URL) window. opener communicates with the parent page. Here we will make a small revision. First, we will use window. when you open (URL), add a parameter to specify the name of the window to open, as shown below:
Function opennewwindow ()
{
VaR url = http://www.website.com/page1.aspx? P1 = V1 & p2 = V2;
VaR winname = "";
VaR win = Window. Open ("about: blank", winname );
Postnewwin (URL, winname );
}
Modify the postnewwin function, pass the handle of the new window to the function, and set the target of the POST request to this parameter, as shown below:
VaR Postnewwin =   Function (URL, winname ){
.........
Form.tar get = Winname ;
.......
};
In this way, you can use both post and get methods.

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.