$ .ajax,$.post或$ .get无法运行,无法使用响应 [英] $.ajax, $.post or $.get won't function, cannot use the response

查看:177
本文介绍了$ .ajax,$.post或$ .get无法运行,无法使用响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
返回AJAX回调返回

Possible Duplicate:
return AJAX callback return

我已经使用AJAX编写了一个jQuery代码,但是尽管我付出了所有努力(尝试$.post$.get $.ajax),但我无法获得任何响应,也无法在其中使用它我的代码. 示例代码:

I have made a JQuery code using AJAX, but despite all my efforts (trying $.post, $.get and $.ajax) I am not able to get any response nor using it in my code. Example code:

var r = $.post("ajaxpage.php", function(data) {return data});
console.log(r); // nothing

推荐答案

对于Java和AJAX,初学者最常见的误解之一是将AJAX请求视为正常"函数,进行变量分配或期望使用立即在其代码中进行AJAX响应.

One of the most common misconceptions of beginner programmers about Javascript and AJAX is to treat the AJAX request as a "normal" function, doing variable assignments or expecting to use immediately the AJAX response in their code.

这不是它的工作方式;让我们尝试了解正确实施的工作原理.

That's not how it works though; let's try to understand how a correct implementation works.

AJAX 代表异步JavaScript和XML .

一会儿我们将忽略XML部分(这是关于如何通过XMLHttpObject进行请求的更多内容),其中的Javascript位非常明显(使用或不使用JQuery的脚本语言),让我们专注于异步.

We will ignore for a while the XML part (which is more about how the request is carried on, via XMLHttpObject), the Javascript bit is quite obvious (it's the scripting language we use, with or without JQuery), let's focus on asynchronous.

以这种方式思考AJAX:这就像在上班时向同事发送工作电子邮件并提供一些信息;并要求采取行动和/或其他信息作为回报;无论您的朋友是否立即回答,您都必须继续工作,因为答案可能会延迟(您的朋友今天可能特别忙). 当您的朋友回答相关信息时,只有 then 您可以解决此问题并采取适当的措施.如果要求采取措施,则您的同事只有在收到您的电子邮件后才会继续进行操作.

Think about AJAX this way: it is roughly like sending a work email to a colleague, while you're at work, and giving some info; and requesting an action to be taken and/or other info in return; no matter if your friend answers immediately or not, you still have to continue your job because the answer could be delayed (your friend may be particularly busy today). When your friends answers with relevant info, only then you will address the matter and take appropriate actions. If an action is requested, your colleague will carry it on only when he receives your email.

AJAX的工作方式类似:从客户端服务器端页面,您都会进行 request 请求, action (例如在数据库中写一行)或 response (某些数据是从您所调用的页面在服务器端获取的.)

AJAX works in a similar way: from client-side, you make a request to a server-side page, and expect an action (like writing a row in a database) or a response (some data fetched server-side from the page you are calling.

重要的一点是,您的JavaScript代码将继续执行,而不管往返是否完成,因为您的客户端可能会明显延迟(请记住JS运行客户端)对服务器进行AJAX调用(服务器速度慢,连接速度慢等).

The important bit is that your JavaScript code continues its execution regardless of the completion of the round-trip, as there can be a significant delay when your client (remember that JS runs client-side) makes an AJAX call to the server (slow server, slow connection, and so on).

所以您不能期望做类似的事情:

So you can't expect to do things like:

var r = $.post("ajaxpage.php", function(data) {return data});

因为$.post实际上没有像常规"函数那样返回"一个值,所以变量r不能由$ .post

because $.post doesn't actually "return" a value like a "regular" function, the variable r cannot be valued by $.post

相反,您应该基于done事件来组织代码,该事件在AJAX调用完成后触发:保证(如果没有失败,例如服务器端错误) ,未找到的页面等),如果成功,我们实际上会在您的代码中使用该响应. 通过该事件,我们可以调用一个将响应作为参数接收并实际使用的函数.

Instead, you should organize your code based on the done event, which is fired after the AJAX call has completed: it guarantees (if there have been no failures, like server-side errors, not found pages, etc.) that we actually have a response to use in your code on success. With that event we can call a function that receives the response as a parameter and actually uses it.

$.post("ajaxpage.php").done(useMyResponse);
// rest of the code

function useMyResponse(response)
{
    // do fancy things with the response like:
    console.log(response); // works!
    // or maybe
    $("#someElement").html(response);
}

为此的简写为:

$.post("ajaxpage.php",function (response) { 
     // use response
     console.log(response);
});

我不是英语为母语的人,请原谅我的错误.

i'm not a native English speaker, forgive my mistakes.

这篇关于$ .ajax,$.post或$ .get无法运行,无法使用响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆