Asynchronous javascript-ajax

Source: Internet
Author: User
Does the callback function in ajax directly execute the code in the function after returning data and executing the code in the function? In the second case, how can we avoid the troubles caused by asynchronization?

Excuse me ajaxThe callback function is Return data and execute the code in the function.Or Directly execute the code in the function without returning data.? In the second case, how can we avoid the troubles caused by asynchronization?

Reply content:

Excuse me ajaxThe callback function is Return data and execute the code in the function.Or Directly execute the code in the function without returning data.? In the second case, how can we avoid the troubles caused by asynchronization?

The callback function of success is the function to be executed after data is successfully returned, that is, it will be executed only after data is obtained.
In other cases, there are callbacks such as beforeSend (before sending), complete (after the request is complete), and error (request error ).

As for the troubles caused by asynchronous communication, I don't quite understand. can I give an example?

It must have returned data and then executed the code in the function =If no data is returned, execute =. is it not waiting for an error to be reported.

ajaxFull nameAJAX = Asynchronous JavaScript and XML(Asynchronous JavaScript and XML ).
First, answer your question. when success is returned, the function will be executed only after data is successfully returned. If no response is returned, it will wait for a while, which is generally the network delay time. You can also set it yourself.

In the following examplejqueryFrameworkajaxTo put it simply.
Generally, I will use the name + value format in this way.

$.ajax({name:value, name:value, ... })
  • asyncBoolean value, indicating whether the request is processed asynchronously. The default value is true.If this parameter is set to false, asynchronous mode is disabled. when a request is made, the interface is very slow.

  • beforeSend(xhr)The function that runs before sending the request.

  • Indicates whether the browser caches the requested page. The default value is true.

  • complete(xhr,status)The function that runs when the request is complete (called after the request succeeds or fails, that is, after the success and error functions ).

  • The content type used when contentType sends data to the server. The default value is "application/x-www-form-urlencoded ".

  • Context specifies the "this" value for all AJAX-related callback functions.

  • dataSpecifies the data to be sent to the server.

  • DataFilter (data, type) is a function used to process the original response data of XMLHttpRequest.

  • dataTypeThe expected server response data type.

  • error(xhr,status,error)The function to be run if the request fails.

  • Global Boolean value that specifies whether a request triggers a global AJAX event handler. The default value is true.

  • Boolean value of ifModified, specifying whether the request is successful only when the response changes since the last request. The default value is false.

  • In jsonp, rewrite the callback function string.

  • JsonpCallback specifies the name of the callback function in a jsonp.

  • Password specifies the password used in the HTTP access authentication request.

  • A Boolean value of processData that specifies whether the data sent by the request is converted to a query string. The default value is true.

  • The character set specified by scriptCharset.

  • success(result,status,xhr)The function that runs when the request is successful.

  • timeoutSet the local request timeout (in milliseconds ).

  • A Boolean value of traditional that specifies whether to use a traditional style serialized by parameters.

  • typeSpecifies the request type (GET or POST ).

  • urlSpecifies the URL of the request. The current page is used by default.

  • Username specifies the username used in the HTTP access authentication request.This can simulate user login, and add password, you know.

  • Xhr is a function used to create an XMLHttpRequest object.

The red part is commonly used by me.

The success function is called back after the requested interface responds to the data. Therefore, as long as the server processes the data properly, the data returned is the data you want.

I will not repeat the answer to the question. I just want to ask what troubles you have mentioned about asynchronization.

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.