ajax function msg,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是簡單的:

$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函數中適當的類型。

#2

1

You have to echo or print the variable since jquery fetches the output of your script. A simple return doesn't produce any output.

由於jquery獲取腳本的輸出,所以必須回顯或打印變量。一個簡單的返回不會產生任何輸出。

$text="text";

echo $text;

?>

#3

1

You need to check jQuery mannual for function $.post,the success param is a function and have several params .The most common used param is msg which is the output by the request url. you need to know the param msg is the output of request url(do_history.php),if you use return in do_history.php,the content($text) will not output to browser so the msg param will contain nothing.but if you use echo ,print etc, the content($text) will output to browser so the msg param will contain the value!

您需要檢查jQuery mannual的函數$。post, success param是一個函數,有多個param,最常用的param是msg,它是通過請求url輸出的。如果在do_history中使用return,則需要知道param msg是請求url(do_history.php)的輸出。php內容($text)不會輸出到瀏覽器,所以msg param將不包含任何內容。但是如果您使用echo、print等,內容($text)將輸出到瀏覽器,所以msg param將包含該值!

#4

0

You need to output text on php side (print 'text'; // echo 'text')

需要在php端輸出文本(打印“text”;/ / echo '文本')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值