An Ajax Error

Source: Internet
Author: User

Let's talk about the story and the idea.

A friend of mine is a website and uses jquery's Ajax method to load an HTML file from the backend.CodeThen, it is dynamically inserted into the DIV component of the webpage. This is so common. Jquery's powerful load method can accomplish this. This is the code written by a friend:

 
VaR tab = jquery ("# dynamic_tab"); var url = "/list_ajax/"; tab. Load (URL );

It is as simple as it is. There is no problem in running chrome, Firefox, and safari. Only IE is not good, whether it is IE7, IE8, or ie9. The problem is that there is no problem accessing the Ajax link using IE, but the "undefined" respons object is returned in jquery's Ajax method. No error reported!

I couldn't solve the problem, so I had to google it -- "jquery load ie". A lot of people are asking this question. So we started the shotgun programming method.

Stackoverflow was browsed for 33 K times. This problem occurs: jquery's. load () not working in IE-but fine in Firefox, chrome and Safari, the answer is not checked (unreliable), stackoverflow has many people asking similar questions, but there is no answer. Let's try it out, shotgun. I tried it for half a day.

Then I checked it on Google and saw some people talking about the IE cache problem. What should I do? Set the cache to false or use the following method to solve it:

VaR tab = jquery ("# dynamic_tab"); var fuckie = math. Random (); var url = "/list_ajax/" + "? Fuckie = "+ fuckie; tab. Load (URL );

It is still the same. It does not work. Almost all of them have tried and won't work. After a day, a friend angrily said, "Microsoft should close down quickly. The product is too bad ". IE is really bad.

So after I use the Web debugger of ie9 to view the Ajax link,IE has an HTTP Ajax request to the website. You can also see that the request is returned, but it is not displayed on my page -- jquery's Ajax responsetext is undefined!

I am not familiar with jquery, the old guy. I have to start debugging jquery code and want to see what has been done in it. What error has been reported? After an hour of calling, I basically understood the Ajax encapsulation of jquery, but I still couldn't find the problem.

So I had to set up the original ajax to see what the Ajax ActiveX object of IE has done? I wrote the following code (this is the way Ajax was written in the past, so it is not difficult, and there are still routines on the Internet to copy ):

Function initajax () {var Ajax = false; try {Ajax = new activexobject ("msxml2.xmlhttp");} catch (e) {try {Ajax = new activexobject ("Microsoft. XMLHTTP ") ;}catch (e) {Ajax = false ;}} if (! Ajax & typeof XMLHttpRequest! = 'Undefined') {Ajax = new XMLHttpRequest ();} return Ajax;} var Ajax = initajax (); Ajax. open ("get", URL, true); Ajax. onreadystatechange = function () {If (Ajax. readystate = 4 & Ajax. status = 200) {var show = document. getelementbyid ("haochendiv "). value; show. innerhtml = Ajax. responsetext;} Ajax. send (null );

I still don't know why I didn't see any errors reported by IE. However, I can be sure this is not a problem with jquery. It is estimated that it is our own.Program. However, at this time, the program is too easy to debug. During debugging, in ie9 mode, we found that the native ie Ajax object is in the onreadystatechange function, and its responsetext is like the following:

What is"System Error:-1072896658."? Google checked that a bunch of pages are basically garbled, that is, I don't know the encoding of the webpage returned by the Ajax backend program. Charset = UTF-8 must be added to the returned HTTP header.

Therefore, modify the backend Ajax program and specify the charset in the returned HTTP header, So Ie will work normally and switch back to jquery's load code, everything works properly (the back-end program is originally in UTF-8 encoding format, but it is not explicitly specified in the HTTP header, but only IE will not detect it automatically ).

The reason for this problem is that we did not write the webpage according to the specifications. As a result, there are too many HTML specifications to remember. But you may knowThere is a website called http://validator.w3.org that can help you verify a lot of nonstandard things on your web page. This tool will report a lot of errors, many of which are a bit of nitpicking. However, let's take a look (Note: Today's coolshell has installed many plug-ins and I have called some of them, so there are many errors. I still remember that I did not report any errors when there were no plug-ins or custom styles ).

Postscript

I will share this question and process with you for the following purposes:

1) Many people are reporting this problem online, but basically cannot find the answer (including stackoverflow). So I will share it and fill in the gaps.

2) I believe that our programmers are experiencing such things every day. I don't know how everyone will do things like this? Maybe most people check various solutions on the Internet and try them one by one until they get the chance of shotgun programming. Of course, most answers are possible. HoweverWhen we find the answer, will we go into depth to understand the specific cause of this problem and think about things around it in a single way??

3) In addition, in today's age of N multi-framework, N multi-lib, and N multi-open source,I wonder if you have lost your ability to write code from scratch?For example, I don't know how many people will write their own original Ajax? However, I suggest you understand the most basic knowledge and solutions when using various frameworks. It is really important to know what it means.

I was a programmer from the "Age of eating". At that time, programmers had to do everything on their own and worked very hard. Today, I told many people about my past experiences, it will be a joke, but I have benefited a lot from my experience in the Age of doing everything myself. I will share my thoughts with you and hope they will be useful to you.

(Full text)

Original post address

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.