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

时间:2012-07-19 15:00:48

标签: 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”)被发送到框架中的模板,并显示了模板。 这就是为什么。 固定。 谢谢。 所有人。

2 个答案:

答案 0 :(得分:1)

$.ajax({
  type:"POST",
  url:"index/success",
  async:false,
  data:{ name:name, password:password},
  success:function(html){ // <------- you need an argument for this function
    // html will contain all data returned from your backend.
    console.log(html);
  }
});

答案 1 :(得分:1)

假设你正在使用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);
    }
});

享受并祝你好运!