php ajax成功:函数(msg) -获取msg
I have some jquery/php code which uses ajax to call another page.
我有一些jquery/php代码,它使用ajax调用另一个页面。
var pall_id = $(this).attr('id');
$.ajax({
type: "POST",
url: "do_history.php?pall_id="+pall_id,
success: function (msg) {
alert (msg);
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert('Error submitting request.');
}
});
However what do I do to get the value of msg? e.g. if do_history.php is simply:
然而,我怎么做才能得到味精的价值呢?例如,如果do_history。php是简单的:
<?php
$text="text";
return $text;
?>
would 'msg' not be "text" so when I alert(msg); I would get "text" popping up on my screen.
“msg”不是“text”,所以我提醒(msg);我会在屏幕上弹出“文本”。
What do I need to do to return a string value? Any ideas?
要返回一个字符串值,我需要做什么?什么好主意吗?
Thanks,
谢谢,
4 个解决方案
#1
1
Use echo $text;
and in your $.ajax
options add dataType: 'text'
.
使用echo $文本;和你的美元。ajax选项添加数据类型:“文本”。
However, a better solution would be using dataType: 'json'
and then echo json_encode($text);
- in this case $text
could also be an array/object/number and it would be the appropriate type in the JavaScript function .
但是,更好的解决方案是使用数据类型:'json',然后回显json_encode($text);在这种情况下,$text也可以是一个数组/对象/数字,它将是JavaScript函数中适当的类型。
更多相关文章
- jQuery——将title属性用作悬停的文本,但只在同一父类中使用。
- 如何在java脚本中获取节点内部文本?
- jquery 监控文本框键盘事件(回车事件),附常用keycode值。
- 使用jQuery更新textarea值更改的文本
- 输入-文本对齐:用jQuery右填充输入
- 如何使用javascript/jquery获取文本框数组值
- 解析PHP的基本文本输出
- 在体html中搜索文本的最佳方法是什么?
- 通过ajax POST提交nicEdit文本编辑器的值,不起作用