Replacement of the url length of a jsonp request (ajaxcdr)

Source: Internet
Author: User

Problem description: The url is too long when jsonp is used across domains.

Explanation:

For cross-origin requests, if a jsonp request is used (for details, see the summary of cross-origin issues in the interaction between MVC4 Web APIs and Ajax), jsonp is a get request because it is a script tag of the request, of course, the get request parameters are passed after the url. Because the browser has a length limit on the url, when you submit an article, the url length will be too long. Request failed

Solution:

Because I used the uploadify plug-in to upload files, it was implemented by flash cross-domain submission (based on the security sandbox policy file crossdomain. xml, details: Let's go back and talk about Uploadify's cross-origin upload principle), so I think it must be useful for flash to solve the problem of cross-origin requests. I checked the "flash ajax plug-in" on google.AJAXCDR: uses Flash to perfectly solve JavaScript and AJAX cross-origin http post/GET form requests [original]The blogger implemented cross-origin flash requests (get, post ).

Usage:

1. Place the crossdomain. xml Security Sandbox policy file in the root directory of the requested site. For details (Flash Security Sandbox and cross-domain files)

2. The (ajaxcdr contains two files) page introduces ajaxcdr. js (must be introduced before the </body> Terminator. Otherwise, it cannot take effect in IE. You may also be prompted about the flash version.) search for "var swfname.zip" in ajaxcdr. js to replace the value with the path of ajaxcdr.swf. For example, var swfName = "/Content/ajaxcdr/ajaxcdr.swf ";

Extension:

1. The ajaxcdr. js file has a method defined as "$ ()" in conflict with Jquery. Change it to "ajaxcdr" or "others", and change it to the same one when calling the code in about 56 lines below.

2. The third parameter of the request sending function AjaxCrossDomainRequest defined in this file must be formname. If there is no parameter or it is not convenient to use the form, the following modifications can be used to transfer the parameter. Rewrite the AjaxCrossDomainRequest method as follows:

1 function AjaxCrossDomainRequest (url, method, formnameordata, callback) {2 method = js_strtoupper (method); 3 AjaxCrossDomainResname = callback; 4 var contentType = "application/x-www-form-urlencoded"; 5 var body = ''; 6 if (formnameordata =" "| formnameordata. indexOf ('=')> 0) {body = formnameordata;} // judge whether it is form or data (11 = 22 & 22 = 33) 7 else {8 var formname = formnameordata; 9 var form = document. f Orms [formname]; 10 for (var I = 0; I <form. length; I ++) {11 // if it is a single choice button, check box, single choice drop-down box 12 if (form. elements [I]. type = "radio" | form. elements [I]. type = "checkbox" | form. elements [I]. type = "select") {13 if (form. elements [I]. checked & form. elements [I]. name! = "") {14 body + = encodeURI (form. elements [I]. name) + '=' + encodeURI (form. elements [I]. value) + '&'; 15} 16} 17 // select the drop-down box 18 else if (form. elements [I]. type = "select-multiple" & form. elements [I]. name! = "") {19 for (var sm = 0; sm <form. elements [I]. length; sm ++) {20 if (form. elements [I] [sm]. selected) {21 body + = encodeURI (form. elements [I]. name) + '=' + encodeURI (form. elements [I] [sm]. value) + '&'; 22} 23} 24} 25 // Button, Hidden, Password, Submit, Text, Textarea, and other Text types 26 else {27 if (form. elements [I]. name! = "") {28 body + = encodeURI (form. elements [I]. name) + '=' + encodeURI (form. elements [I]. value) + '&'; 29} 30} 31} 32} 33 var fs = FlashHelper. getFlash (); 34 // fs. loadPolicyFile ("http: // domain/blah/crossdomain. xml "); 35 if (js_substr (body,-1, 1) =" & ") {36 body = js_substr (body, 0,-1 ); 37} 38 if (method = "GET") {39 // GET Request method 40 var urlget = ""; 41 if (js_substr (url,-1, 1) = "? ") {42 urlget = url + body; 43} 44 else if (js_strpos (url ,"? ")> 0 & js_strpos (url," = ")> 0) {45 urlget = url +" & "+ body; 46} 47 else {48 urlget = url + "? "+ Body; 49} 50 fs. xmlHttp (urlget, "displayResponse", method, "", contentType); 51} else {52 // POST Request method 53 fs. xmlHttp (url, "displayResponse", method, body, contentType); 54} 55}

Modified file: http://files.cnblogs.com/woyouwozai/ajaxcdr.rar

In this way, if you want to pass in the form parameter, you can pass in a form name. If you do not want to pass the value, the formnameordata parameter will be passed blank (""). if it is a self-assembled parameter, the parameter is like the parameter "11 = 22 & 22 = 33.

However, there is still a problem. If the parameter contains "&", the problem may occur, for example, "11 = 22 & 22 = 3 & 3". In this way, it will be parsed into three parameters "11, 22 "., 3 ", this problem occurs when I submit content containing rich text boxes. When two consecutive spaces are source code," & nbsp; "directly caused 500 error. Finally, you have to replace" & "in the edited content in the rich text box with" @ "or others (templatecontent = templatecontent. replace (// g, '@');) in the background and then replace it. If not, use fromname to submit it.

Call method: AjaxCrossDomainRequest (url + "/Add", "POST", "Guid =" + $ ("# mailguid "). val (), "alert (AjaxCrossDomainResponse );");

So far, you can safely use it.

Another friend said on the Internet that "the original ajaxcdr. js encoding format ANSI encoding, need to change this encoding format to UTF-8 encoding format. Otherwise, the fs. XmlHttp "object doesn't support this property or method" error will appear in IE.

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.