A summary of the reasons for using jquery Ajax to fail in IE

Source: Internet
Author: User
Tags comments html page

1, and then a variety of debugging, and finally found: IE Gatekeeper, and then open $.get (). will be invoked, and then the second time the call is found again. So I infer that the problem of IE caching, the IE cache after the removal, sure enough. But customers cannot know how to clean up the cache. So I had to find a way.

On the internet found that the big friends have given a method is to use jquery with the method:

The code is as follows Copy Code

$.ajaxsetup ({cache:false});

In addition, the analysis is that IE is based on the requested URL is not the same to send the request, for the same request, ie send only one HTTP request, so the same request sent multiple times, but IE is not actually sent.

With this reason: then it is good to do, you can add a timestamp after the URL.

The code is as follows Copy Code
$.get (
"Imgsupload",
{Imgspath:path,<span style= "COLOR: #333399" >timestamp:new Date (). GetTime () </span>},
function (data) {

}
);

2, the following questions are personal issues, but the request can be sent to the background, and also executed, and returned. However, the Ajax callback method cannot be performed.

But it is positive in other browsers. Analysis returned to come over the data and found the mess. That's the wrong type of data returned, I'm returning JSON, but I think it's all returned as a string, so I have the following code on the server side:

The code is as follows Copy Code
<span style= "COLOR: #333399" >response.setcontenttype ("Text/plain;charset=utf-8");
</SPAN>

Ff,chrome is normal, but IE can only be identified as text, cannot parse into JSON, all without invoking the JSON method

After testing found that you can not set the message header, of course, it is also possible to set, but not text/plain, but:

Response.setcontenttype ("Application/json;charset=utf-8");


jquery Ajax is invalidated under IE

One page needs to use jquery Ajax to get the data, the chrome and FF can get and display data correctly, the code snippet is as follows:

  code is as follows copy code

  $. Ajax ({

 


    URL: "/item/getcomments",

 

    DataType: "JSON",

 

    data: {"IID": "123456", "Nick": "Xlight"},

 

p>    success:function (data) {

 

        $ (". Comments-total "). HTML (data.total);

 

        $ (". Comments-body"). HTML (data.list);

 

   }

 

});

Check with the Debug tool with IE, the display data has been successfully obtained (STATUS:200 and have data to return), that's strange ...

Add an error callback to Ajax:

  code is as follows copy code


  $ . Ajax ({

 


    URL: "/item/getcomments",

 

     DataType: "JSON",

 

    data: {"IID": "123456", "Nick": "Xlight"},

 

    success:function (data) {

 

         $ (". Comments-total"). HTML (data.total);

 

        $ (". Comments-body"). HTML (data.list);

 

   },

 

Here are three parameters in detail see jquery manual

Error:function (A, B, c) {

alert (c);

}

});

IE display Error "error:c00ce56e" information such as Google, the general understanding that this is the reason that IE can not parse the data, the vast majority of the header is caused by malformed coding.

Know the reason to check up also easy, with IE Debug tool looked at the next return data header, there is "Content-type text/html;" Charset=utf8 ", can see here should be utf-8, find back-end code change (header send function, such as PHP header ()), Refresh on OK!

Other issues

1) Server-level steering support such as Apache Urlrewrite. If the test.js?folder=/js/can be resolved to/js/test.js.
2) JavaScript and HTML, like pictures, are cached as static files by browsers. Then add a random number to ensure that each request is a new file, such as common.js?t=12442344.
3) This parameter is for the overall page query JS parameters used, is a JS external application, but is definitely not for the internal application of JS. If you have an HTML page that reads:
<script type= "Text/javascript" src= "test.js?a=100&b=2" ></SCRIPT>
First, the ? a=100&b=2 is absolutely not going to affect the execution of internal functions of test.js because test.js cannot receive this parameter.
Second, this is the a=100&b=2 that can be accessed through the DOM structure in HTML to this JS path. The following example:
var xsrc = document.getelementsbytagname ("script") [document.getElementsByTagName ("script"). Length-1].src
At this point, the xsrc can get test.js?a=100&b=2, and then the regular expression is used to parse the parameters: A=100 and b=2. Then you take these two parameters to make some judgments.
Finally, looking back at the first one is certainly not possible, and not every server supports Urlrewrite. I think the nature is between the second and the third. Don't think jsver=2.1.1.3626.0 has any practical, at most to determine what version, but this version number is originally from the PHP code to get. I think this parameter is mostly symbolic, and it is of no practical use.

Related Article

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.