jquery - Using $.post within a javascript function, cannot assign value to a variable? -
I am trying to use $ .post inside the function, get the result, then the function is right or wrong Return the result of the $ .post callback, however, the callback appears, after the occurrence of the return of the original work.
Here is the current code, variable variable is always undefined, but if I give a warning within the $ .post callback, it returns the correct result;
function elementexists (key, type, path, api, pid) {var ret; $ .post ('? Do = elExists', {key: key, type: type, path: path, apiid: api, pid: pid}, function (data) {ret = data;}); Warning (Retired); Return writ; }
As you've found that the post has been done in asynchronous manner, If you want to understand it as synchronous, you need to use the method and set the async parameter to false. Although it would be better to accept that this post is asynchronous and your code is created so that the action taken by the callback is independent of the method that invokes the post. Obviously, you really want to do something other than alerts, but knowing that it is difficult to advise how to refactor this.
Edit : I think your code is checking to see if something exists, but it is checking for a purpose. If we knew what the objective was, then it is possible to complete how to do this in the callback that the method should be re-written again.
Comments
Post a Comment