简体   繁体   English

用 AJAX 来回发送信息

[英]sending information back and forth with AJAX

With $.post, you can send information to the server, but what when you need to receive information from the server?使用$.post,你可以向服务器发送信息,但是当你需要从服务器接收信息时怎么办?

How does information change from being of a way that can be held by a php variable to being of a way that can be held by a javascript variable and vice-versa?信息如何从可以由 php 变量保存的方式变为可以由 javascript 变量保存的方式,反之亦然?

This is more relevant to your question: http://docs.jquery.com/Ajax/jQuery.post这与您的问题更相关: http://docs.jquery.com/Ajax/jQuery.post

Alert out the results from requesting test.php (HTML or XML, depending on what was returned).提醒请求 test.php(HTML 或 XML,取决于返回的内容)的结果。

$.post("test.php", function(data){
  alert("Data Loaded: " + data);
});

Alert out the results from requesting test.php with an additional payload of data (HTML or XML, depending on what was returned).使用额外的数据负载(HTML 或 XML,取决于返回的内容)提醒请求 test.php 的结果。

$.post("test.php", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
  });

Gets the test.php page content, store it in a XMLHttpResponse object and applies the process() JavaScript function.获取 test.php 页面内容,将其存储在 XMLHttpResponse object 中并应用 process() JavaScript function。

$.post("test.php", { name: "John", time: "2pm" },
  function(data){
    process(data);
  }, "xml");

Gets the test.php page contents which has been returned in json format ("John","time"=>"2pm"));获取以 json 格式返回的 test.php 页面内容 ("John","time"=>"2pm")); ?>) ?>)

$.post("test.php", { func: "getNameAndTime" },
  function(data){
    alert(data.name); // John
    console.log(data.time); //  2pm
  }, "json");

Check out json_encode() and json_decode() .查看json_encode()json_decode() These are now part of PHP, and allow you to switch back and forth between PHP arrays and associative arrays (or stdClass objects) and javascript arrays or objects (as a JSON literal).这些现在是 PHP 的一部分,允许您在 PHP arrays 和关联 arrays(或 stdClass 对象)和 javascript arrays 文字或对象(作为 8869 808075 )之间来回切换。

Essentially, instead of returning xml or html, you can do echo json_encode($all_my_php_data);本质上,您可以不返回 xml 或 html,而是执行echo json_encode($all_my_php_data); and get back a javascript object.并取回 javascript object。

If you pass 'json' as the type parameter of your $.post(), your success callback will contain the JSON object you have echoed in your PHP script.如果您将“json”作为 $.post() 的类型参数传递,您的成功回调将包含您在 PHP 脚本中回显的 JSON object。

$.post() documentation $.post() 文档

Then you need to receive the content sent back from server.然后你需要接收从服务器发回的内容。 You simply define callback function for $.post with 'data' parameter.您只需使用“数据”参数为 $.post 定义回调 function。 For example:例如:

$.post('/index.php', { key: 'value' }, function(data) { alert(data); });

You can specify type of the returned value so that jQuery can automatically process it.可以指定返回值的类型,jQuery可以自动处理。 If you return JSON value from the PHP script then you should add additional parameter at the end:如果您从 PHP 脚本返回 JSON 值,那么您应该在末尾添加额外的参数:

$.post('/index.php', { key: 'value' }, function(data) {
    alert(data.someItem);
  }, 'json');

But if you need to get data from PHP server without POSTing or GETting anything first, then you need to implement Comet.但是,如果您需要从 PHP 服务器获取数据,而无需先进行 POST 或 GET 操作,那么您需要实现 Comet。 But that's a bit more work:)但这是更多的工作:)

The " callback " portion of the jQuery.post function is what you want to look at.您要查看的是jQuery.post function 的“回调”部分。

In http you perform a request from the client (javascript in web-browser) to the server and then process information returned by the latter.在 http 中,您执行从客户端(web 浏览器中的 javascript)到服务器的请求,然后处理后者返回的信息。 The choice of which way information is represented in both communications is up to you.在两种通信中以何种方式表示信息的选择取决于您。

When using AJAX (possibly through jQuery) you can make a request to a php handler which will be in charge to return the information to the browser, usually formatted as a JSON literal (you can encode the response using json_encode(), as suggested by Brian).使用 AJAX(可能通过 jQuery)时,您可以向 php 处理程序发出请求,该处理程序负责将信息返回给浏览器,通常格式为JSON文字(您可以使用 json_encode() 对响应进行编码,如建议的那样布莱恩)。 Eventually you will parse it on the client (eg using jQuery) to obtain a javascript object.最终您将在客户端(例如使用 jQuery)解析它以获得 javascript object。

(The $.post( url, [data], [callback], [type] ) function will automatically parse the response and yield it to the callback function, whose signature should be callback(data, textStatus) where data is be the parsed object and textStatus reports success or failure status ( jQuery.post ). ($.post( url, [data], [callback], [type] ) function 将自动解析响应并将其产生给回调 function,其签名应为 callback(data, textStatus) 其中数据将被解析object 和textStatus报告成功或失败状态 ( jQuery.post )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM