On cross domain request using Ajax in IE8 / 7

Time:2021-7-18

Project content:Call the API provided by the express company, send the express, store the information in the database, and query by date

Development technology:C#、asp、Javascript、Ajax、Layui,SQLServer

Tools:Vs, postman, IE browser (ie11, IE8 / 7), chrome

Process:

  1. Use layui (iframe version) for layout, use layui because it supports IE browser, can be better compatible, provide date plug-in, more convenient.
  2. Ajax calls the API to send. After sending successfully, return the express number and other information.
  3. The returned information and important information of the goods are transmitted to the background through Ajax and stored in the database.
  4. Using the date plug-in, AJAX retrieves eligible data.

a key:

1. When API is requested, an error is reported: (HTTPS) refer policy: no refer when download

Reason: the server security upgrade does not comply with the new security policy

Solution:

2. In ASP page, send Ajax request, no response (flash by, in fact, it is refreshed)

Reason: there are some problems in ASP

After the button is clicked and the request is submitted, the form will also submit a request and the page will be refreshed

Solution: prevent form submission from refreshing the page, add onsubmit = “return func();”    function func() {  return false;}

3. Ajax request error 500

Reason: no JSON serialization (error)

Solution: JSON. Stringify (data);

4. The parameters that need to be passed in to call API need to be generated immediately (MD5 encryption algorithm)

You need to sort the string array in advance and combine it into a new string (only for this project)

The following is MD5 encryption algorithm (32-bit encryption) C#

//MD5 encryption algorithm sign
    public string UserMd5(string str)
    { 
        string cl = str;
        string pwd = "";
        MD5 md5 = MD5.Create();// Instantiate an MD5 object
        //After encryption, it is an array of byte type. Here, you should pay attention to the selection of utf8 / Unicode 
        byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
        //By using loops, an array of byte types is converted to a string that is formatted with regular characters
        for (int i = 0; i < s.Length; i++)
        {
            //The resulting string is in hexadecimal type format. The formatted characters are lowercase letters. If uppercase (x) is used, the formatted characters are uppercase characters 
            pwd = pwd + s[i].ToString("x2");
        }
        return pwd;
    }

You need to pass the return value in C # into the ASP page: the method in C # needs to add static   WebMethod

5. Garbled Chinese characters in database

Solution: add “n” before the parameter

6. When AJAX is nested, change asynchronous to synchronous

Difficulty: after the code is uploaded to the server, the browser defaults to IE7. The framework is not compatible, and there are problems with Ajax requests (jQuery AJAX is used here)( Make it clear before developing)

1. Ie version is too low, and the referenced jQuery version is changed from 3.0.0 to 1.7.1

Problem and solution: because the version is too low, it does not support the serialization and deserialization of JSON, so you need to import the json2.js file (which can be downloaded from GIT)

2. In IE7 / 8 environment, there is no transfer in Ajax request, and the error is returned directly

Try to solve this problem: add alert (jQuery. Support. Cros); Judge whether cross domain access is supported, ie11 shows true, IE7 / 8 shows false, jquery.support.cros = true is added, IE7 / 8 shows true, but the request still cannot be processed normally.

Solution: finally, jQuery AJAX is abandoned and native AJAX is adopted. First of all, compatibility should be done when using. The code is as follows:

//Create Ajax object for compatibility
        var xhr = null;
        try{
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) { 
            try { 
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
            
            }
        }
        if (xhr == null) {
            xhr = new XMLHttpRequest();
        }
//Request mode, request address
        xhr.open('post', url);
//Request header
        xhr.setRequestHeader('Content-Type', 'application/json');
//Send request
        xhr.send(JSON.stringify(data));  // Here, if you want to send JSON data, you need to serialize it. The lower version does not support it. You need to import the json2. JS file
//Get the data that the server responds to the client
       xhr.onreadystatechange = function () {
         if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    var result = xhr.responseText;
                    var msg = JSON.parse(result);    // To deserialize, you also need to import the json2. JS file. If the jQuery version is too low
              }
         }
       }

 If there is something wrong or can be improved, please comment and welcome criticism and correction. thank you!