如何将数据从 PHP 返回到 jQuery ajax 调用 [英] How to return data from PHP to a jQuery ajax call

查看:36
本文介绍了如何将数据从 PHP 返回到 jQuery ajax 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ajax 发布一些数据.我想操作该数据并返回到调用 jQuery 脚本.

I am posting some data using ajax. I want to manipulate that data and return to to the calling jQuery script.

这是我的 jQuery:

Here is my jQuery:

$.ajax({
  type: "POST",
  url: "somescript.php",
  datatype: "html",
  data: dataString,
  success: function() {
    //do something;
    }
});

这是我在服务器上的 somescript.php:

Here is my somescript.php on the server:

  <?php
    //manipulate data
    $output = some_function(); //function outputs a comma-separated string
    return $output;
  ?>

我在服务器端是否正确执行此操作,以及如何在 ajax 调用完成时访问返回字符串?

Am I doing this correctly on the server side, and how do I access the return string when the ajax call completes?

推荐答案

我想通了.需要在 PHP 中使用 echo 而不是 return.

I figured it out. Need to use echo in PHP instead of return.

<?php 
  $output = some_function();
  echo $output;
?> 

还有 jQ:

success: function(data) {
  doSomething(data);
}

这篇关于如何将数据从 PHP 返回到 jQuery ajax 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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