用户填写输入文本,按下提交按钮。数据发送到服务器进行存储并返回结果。带有结果的 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/