【问题标题】:Want to return a string from PHP to AJAX on success using laravel (or regular PHP)想要使用 laravel(或常规 PHP)成功将字符串从 PHP 返回到 AJAX
【发布时间】:2014-05-16 21:19:28
【问题描述】:

我如何从 PHP 文档中返回一个字符串,然后做一些事情 if(string == 'test') 我应该分配数据类型吗?我应该使用返回还是回声? 这是我目前拥有的。

AJAX 代码:

$.ajax({
                type: 'get',
                url: 'phonegap',
                data: senddata,
                success: function(response) {
                    var a = $(this).html(response);

                    if(a == 'Success')
                        alert("YAY");
                    else
                        alert("NAY");
                },

PHP 代码/LARAVEL

if (Auth::attempt($userdata)) {
                $user = Auth::user();
                //return $user->utid;
                return "Success";
            }
            else {
                return "Error";
            }

【问题讨论】:

    标签: javascript php html ajax laravel


    【解决方案1】:

    你试过 echo 吗?我确定它必须有效

    【讨论】:

      【解决方案2】:

      这样做:

      if (Auth::attempt($userdata)) {
        $user = Auth::user();
        // return $user->utid;
        echo "Success";
        exit;
      } else {
        echo "Error";
        exit;
      }
      

      【讨论】:

        【解决方案3】:

        几个关键的事情:

        -$(this) 在您的success 回调中不引用原始元素

        -使用echo返回响应

        您可以通过以下方式为当前元素设置上下文变量:

        var that = $(this)
        
        $.ajax({
            ...
            success: function(response) {
                var a = that.html(response);
        
                if(a == 'Success')
                    alert("YAY");
                else
                    alert("NAY");
            },
        });
        

        只需 echo 从服务器返回您的回复。

        【讨论】:

        • 那么您的响应没有得到返回。检查控制台并确保请求没有错误
        【解决方案4】:

        在您的 ajax 调用中添加此选项:

        dataType: 'json',
        

        在 php 中,您可以将所需的所有数据放入一个数组中,然后简单地打印出该数组的 json:

        echo json_encode($results);
        

        当你这样做时,javascript 中的响应对象将包含你所有的数组数据。

        例如,如果你定义像$results = array('success' => true);这样的数组,在javascript中你可以检查response.success是真还是假。您可以通过这种方式传递多个值。

        试试这个:

        $.ajax({
            type: 'get',
            url: 'phonegap',
            datatype: 'json',
            data: senddata,
            success: function(response) {
                if (response.success)
                    alert("YAY");
                else
                    alert("NAY");
            }
        });
        

        在 PHP 中

        if (Auth::attempt($userdata)) {
            $user = Auth::user();
            $result = array('success' => false, 'userid' => $user->utid);
        }
        else {
            $result = array('success' => false);
        }
        echo json_encode($result);
        

        【讨论】:

        • 使用这个,我得到了 parseError。我之前也使用 laravel response::json success: function(response) { if(response.success == true) alert("YAY"); 得到了 parseError否则警报(“不”); }
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-14
        • 2015-10-06
        • 1970-01-01
        • 2015-11-24
        • 2018-05-03
        • 2019-05-16
        相关资源
        最近更新 更多