且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的jQuery AJAX函数总是返回false?

更新时间:2022-04-15 22:27:10

check_username调用ajax函数,该函数启动联网操作,然后立即返回. check_username在ajax调用完成和成功处理程序被调用之前很久就返回.因此,成功处理程序与check_username返回的值无关.

check_username calls an ajax function which starts a networking operation and then returns immediately. check_username returns long before the ajax call finishes and the success handler gets called. Thus, the success handler has NOTHING to do with the value that check_username returns.

由于check_username函数本身没有返回值(仅在嵌入式成功处理程序函数中),因此check_username返回undefined这是一个假值,因此您认为它总是返回false.

Since there is no return value in the check_username function itself (only in the embedded success handler function), check_username returns undefined which is a falsey value, thus you think it's always returning false.

如果要对成功处理程序的返回值进行处理,则必须在成功处理程序本身中进行操作,或者必须从成功处理程序中调用另一个函数.这就是异步操作的工作方式.

If you want to do something with the return value from the success handler, then you have to either operate in the success handler itself or you have to call another function from the success handler. This is how asynchronous operations work.

从成功处理程序函数返回truefalse不会执行任何操作.成功处理程序由ajax处理代码的内部调用,并且从成功处理程序返回的内容仅会进入ajax内部组件的内部.成功处理程序的返回值不会以任何方式使用.

Returning true or false from the success handler function does nothing. The success handler is called by the internals of the ajax handling code and returning from the success handler just goes into the bowels of the ajax internals. The return value from the success handler is not used in any way.