php - 在 jQuery 函数中获取结果 PHP 代码?

标签 php javascript jquery

我使用codeigniter,我想用php制作uniqid代码,如下代码(通过random_string()):

function coding(){
    echo random_string('unique');
}

我想在 jquery 函数中使用结果顶部函数(用于 js 代码),我在 jquery 代码中尝试这样做,但是 alert(coding()) 中的输出是“未定义” ”。如何解决?

function coding(){
   $.ajax({
        type: "POST",
        dataType: "text",
        url: "coding",
        cache: false,
        success: function (data) {
            return data;
        }
   })
}
alert(coding()) // in here output is  "undefined" !!!?

最佳答案

function coding(){
   var temp;
   $.ajax({
        type: "POST",
        dataType: "text",
        url: coding,
        cache: false,
        async: false,
        success: function (data) {
            temp = data;
        }
   })
   return temp;
}
alert(coding());

您将收到一个[object XMLDocument]。

关于php - 在 jQuery 函数中获取结果 PHP 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8585876/

相关文章:

php - 强制 heroku 避免使用 https

php - 在 Magento 中向客户网格添加列 - 数据未填充

javascript - 使用jquery将xml转换为对象

JavaScript - 为 <a> 的 onclick 属性添加了无需点击即可运行的功能

javascript - jqGrid 中的动态列和使用下拉列表

php - 无法将 Yii 与本地 SQL Server 2012 连接

php - Symfony/Doctrine "refers to the owning side field which does not exist"- 但类中存在属性

javascript - React Native - 创建形状的 flex 边缘

javascript - 用于更新 JSON 文件的 Node.JS 服务器端脚本

javascript - 使用javascript循环遍历复选框