开发者

php ajax success: function (msg) - get msg

I have some jquery/php code which uses ajax to call another page.

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:

<?php

$text="text";
return $text;

?>

would 'msg' not be "text" so when I alert(msg); I would get "text" popping up on my screen.

What do I need to do to return a string value? Any ideas?

Thanks,


Use echo $text; and in your $.ajax options add dataType: 'text'.

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 .


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

<?php

$text="text";
echo $text;

?>


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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜