javascript - 将变量从服务器传回 ajax 成功

标签 javascript jquery ajax fancybox

用户填写输入文本,按下提交按钮。数据发送到服务器进行存储并返回结果。带有结果的 Fancybox 窗口出现。我的问题是:如何在fancybox中显示结果$res1?

$.ajax({
    type:"POST",
    url:"index/success",
    async:false,
    data:{ name:name, password:password},
    success:function ()
    {
        var html='$res1 from the server should be here instead of this string';//var html=$res1.
        $.fancybox(
            {
                content: html,//fancybox with content will be displayed on success resul of ajax
                padding:15,
            }
        );
    }
});

=========================

好的,仍然不起作用(在 fancybox 中返回整个页面 + 顶部的单词“hello”而不是消息“hello”)。以下是我对以下答案的更新,但无法正常工作:

PHP:

<?php
    $res1="hello";... // handle logic here
    echo $res1; // print $res1 value. I want to display "hello" in the fancybox.
?>

AJAX

$.ajax({
    type: "POST",
    url: "index/success",
    async: false,
    data: {
        name: name,
        password: password
    },
    success: function (html) {
       $.fancybox(
       {
         content: html,//returns hello+page in the fancybox

          //if I use the string below instead of the upper one, the fancybox shows "The requested content cannot be loaded. Please try again later."
         // content: console.log(html) 


         padding:15,
    }
});

============= 新更新: 固定的!!!问题是数据(上例中的“hello”)被发送到框架中的模板,并显示了模板。 这就是为什么。 固定的。 谢谢。 大家好。

最佳答案

假设您使用的是 PHP:

PHP:

<?php
    ... // handle logic here
    echo $res1; // print $res1 value
?>

AJAX:

$.ajax({
    type: "POST",
    url: "index/success",
    async: false,
    data: {
        name: name,
        password: password
    },
    success: function (html) {
        // given that you print $res1 in the backend file,
        // html will contain $res1, so use var html to handle
        // your fancybox operation
        console.log(html);
    }
});

祝你好运!

关于javascript - 将变量从服务器传回 ajax 成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11563646/

相关文章:

javascript - jquery 在其他事件处理程序中调用事件处理程序函数

javascript - Sequelize 按日期分组,忽略小时/分钟/秒

javascript - 如何为 JQuery $.ajax 同步请求设置超时并执行某些操作

asp.net - 不使用 AJAX 的 ASP.NET MVC 中的下拉列表 onchange 事件

javascript - 给定一组 Promise,我如何强制解决最后一个 Promise 的响应,同时确保所有 Promise 成功解决?

javascript - AngularJs $broadcast 无法加载

html - 当光标移动到 Firefox 中的选项时,在 <select> 上触发 mouseleave

javascript - jquery-取消选中下拉更改的复选框不起作用

javascript - Bootstrap 表单验证 - 使用单击事件验证表单

java - 为什么我的Dojo.xhrGet调用错误Call Back方法