Ajax从php获得返回值? [英] Ajax get a return value from php?

查看:101
本文介绍了Ajax从php获得返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从php方法警告返回值,但没有任何反应。这是ajax和php方法。谁能看到我做错了什么?

I want to alert the return value from a php method, but nothing happens. Here is the ajax and php methods. Can anyone see what I am doing wrong?

----------------------------------- --- ...
Ajax脚本

--------------------------------------… Ajax script

$.ajax({
    type: 'get',
    url: '/donation/junk/4',
    data: datastring,
    success: function(data) {
        alert(data');
    }
});

--------------------- ----------------- ...
php方法

--------------------------------------… php method

function junk($id)
{
    return "works11";
}


推荐答案

在PHP中,你可以'只需返回您的值并将其显示在ajax响应中。你需要 print echo 你的最终价值。 (还有其他方法,但是这个主题已经过时了。)

in PHP, you can't simply return your value and have it show up in the ajax response. you need to print or echo your final values. (there are other ways too, but that's getting off topic).

此外,你的 alert()中有一个尾随撇号调用会导致错误,应该删除。

also, you have a trailing apostrophe in your alert() call that will cause an error and should be removed.

修正:

$.ajax({
    type: 'get',
    url: '/donation/junk/4',
    data: datastring,
    success: function(data) {
        alert(data);
    }
});

PHP:

function junk($id)
{
    print "works11";
}

这篇关于Ajax从php获得返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆