Get and post in jquery, Ajax question description for return values

Source: Internet
Author: User

A: Preface

Today I am leaving the company to go back to school to prepare for the exam, before I go, I do a module test in addition to some small bugs. The problem is as follows

I insert data into the database, first of all to select a level menu, and then a more level menu to generate a sub-directory of the first level menu, select the date. Save after filling out the data. When you click Save, you need to verify the new data, which is based on the number and date of the first-level directory to query, if there is a pop-up box, tell them that the date has been changed to the table data. This is where I need to verify. But I didn't use it when I was using the Jquery.get method to write. And at this time I quickly leave work, made me very irritable, and finally a big brother to help me solve the problem.

Two: Problem code:

function Checkform () {      $.get ("url", {' parameter 1 ': A, ' parameter 2 ': b},function(data) {         if(" Success "==data" {/// return success indicates that there are data changes in the database, so the return is False              showtips ("The data is already in the database, please look for changes if needed");                returnfalse;}}); return true ;}

The code I'm doing here is true, which means I do the insert every time. This verification is basically useless.

So I use IE and Firefox browser debugging is useless, and did not report errors. Before leaving work, found a big brother of another company (I was in the outside, several companies together), he looked for a while, this is the asynchronous and synchronization caused, just at the beginning I do not believe, because the code execution to return where the basic no response, I do not think it is asynchronous or synchronization caused. Big Brother says this requires Ajax, which is specified as synchronous because the post and get of jquery are asynchronous. Finally we changed the code to the following:

functionCheckform () {varflag=true; $.ajax ({URL:"/xxx/xxx.do"},data:{' parameter 1 ': A, ' Parameter 2 ': ' B '},async:false, Success:function(data) {if("success" = =data) {Flag=Fasle;}});if(flag==false) {showtips ("XXXXXX"); return false;}}

In fact, this code is a comparison, it is known that the difference between synchronous and asynchronous, just in the online also looked up the following information is said: If it is asynchronous, have not had time to assign value, it has been return. Synchronous is to say that Ajax has a return value before executing the following code. In fact, these should also be.

My own understanding now is to know that there will be a return value in sync, two can not return with async, in the asynchronous code, return words only return to function (data), but still in the $.get ("", "", Function (data)) inside, There is no way out of it at all. This is my own little understanding.

Three: summary

   own It road is just beginning, also want to learn to help others, like that Big Brother kind, he gave me a lot of help, I asked him many questions, his experience is very rich. Nice people have good news, hope in the future it road, can have more experienced person can give us this write rookie help. Some time ago in a group of QQ and some other friends also gave me a lot of help. His way is still very long. Try, come on. Go, tomorrow back to school to prepare for the final exam, this year's junior, next year seniors, hope in this period of time can enrich themselves, for their future out to lay a solid foundation

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.