JQuery. form Ajax no-refreshing upload error (jQuery. handleError is not a function) solution: jqueryajaxform

Source: Internet
Author: User

JQuery. form Ajax no-refreshing upload error (jQuery. handleError is not a function) solution: jqueryajaxform

Today, when using ajaxfileupload, firebug reported a "jQuery. handleError is not a function" error. Because jQuery. form has never encountered this problem. I compared it with previous projects and found that jQuery is 1.10.2 in this project, but 1.4.2 was previously used. After du Niang, find the solution: jQuery. handleError is not a function. The error cause is:
HandlerError only exists in versions earlier than the jquery-1.4.2 and does not exist in Versions later than the jquery-1.4.2.

Therefore, this function is added in the jquery advanced version to solve the problem. This js Code segment can be added to jquer. js or ajaxfileupload. js.

; (function ($) {            jQuery.extend({                handleError: function (s, xhr, status, e) {                    if (s.error) {                        s.error.call(s.context || s, xhr, status, e);                    }                    if (s.global) {                        (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]);                    }                },                httpData: function (xhr, type, s) {                    var ct = xhr.getResponseHeader("content-type"),            xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,            data = xml ? xhr.responseXML : xhr.responseText;                    if (xml && data.documentElement.tagName == "parsererror")                        throw "parsererror";                    if (s && s.dataFilter)                        data = s.dataFilter(data, type);                    if (typeof data === "string") {                        if (type == "script")                            jQuery.globalEval(data);                        if (type == "json")                            data = window["eval"]("(" + data + ")");                    }                    return data;                }            });



Reference (ajaxfileuploadjs) ajaxfileuploadjs reports this error jQueryhandleError is not a function

When I was doing ajaxFileUpload, I also encountered this problem. At the same time, I had other problems. I spent one afternoon solving them:
Question 1: As the landlord said, jQuery. handleError is not a function is because handlerError exists only in versions earlier than the jquery-1.4.2, and does not exist in both jquery-1.6 and 1.7, so this function is copied to ajaxFileUpload in 1.4.2. solve the problem in js
HandleError: function (s, xhr, status, e ){
// If a local callback was specified, fire it
If (s. error ){
S. error. call (s. context | s, xhr, status, e );
}

// Fire the global callback
If (s. global ){
(S. context? JQuery (s. context): jQuery. event). trigger ("ajaxError", [xhr, s, e]);
}
},
Problem 2: the error is always returned and the specified success method cannot be executed. By tracking the execution process of ajaxFileUpload, it is found that when calling its own uploadHttpData function, when executing if (type = "json") eval ("data =" + data );
An exception is thrown, causing status = "error" to be executed during exception handling. Therefore, the error method is always executed.
Online query, I learned that the eval function is used to execute a piece of js Code, rather than reverse decoding the json string as I thought.
Eval ("data =" + data); Means to assign data to the data parameter, but when I return a simple string to the page, such as "OK, in this way, an exception is thrown. Finally, change it to eval ("data = \" "+ data +" \ ""). The returned data is enclosed in double quotation marks as a string and then assigned to data. Finally succeeded...
Post it, hoping to help others who have encountered the same problem.

Ask a php question: jquerypost (url, data, function, type) and ajaxform () in jquery ()

This is jquery's problem .....

The ajaxform you mentioned should be a plug-in. jquery itself does not have the ajaxform function. jquery has ajax, post, get, and other ajax request processing functions, among them, post and get are simplified ajax functions, and their functions can be implemented by ajax functions.

The data parameter of the post function is equivalent to the data when the form is post. It is used to simulate post with js. The data format is {key: value ,...} in this way, these jquery api documents are described in detail.

Ajaxform is a plug-in encapsulated by others. It should be used to facilitate the verification prompts during form submission. The principle must be the jquery. ajax function.

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.