ajaxform给了我错误的回答

时间:2017-12-25 04:56:19

标签: javascript php jquery html ajax

我有以下形式,登录到instagram取决于响应,如果它登录意味着“确定”,那么成功,如果不是它应该给我其他状态。

但在我的情况下,由于某些原因它总是给我空状态。

我试图以失败的尝试来解决它。

$(document).ready(function() {
            $("#msform").ajaxForm({
                beforeSubmit: function(data, frm, opt) {
                    $("#login_submit").css('background', '#A3D780');
                    $("#login_submit").enable(false);
                    $('#divMsg').html('Doing the magic, please wait!');
                    return true;
                },
                success: function(response, statusText) {
                    $("#login_submit").css('background', '#66bd2b');
                    $("#login_submit").enable(true);

                    if(response == null) {
                        $('#divMsg').html("Service is not available at this moment, Please try later. Sorry for inconvenience. Thank you.");
                    } else {
                        if(response.status == 'ok') {
                            $("#msform").html('<fieldset><p class="form-field input-label" style="padding-bottom: 15px">Thank you! Your request is being processed. Your order should arrive within 3-5 business days!</p></fieldset>')
                        } else {
                            $('#divMsg').html(response.message);
                        }
                    }
                },
                error: function(a, b) {
                    $('#divMsg').html("Service is not available at this moment, Please try later. Sorry for inconvenience. Thank you.");
                    $("#login_submit").css('background', '#66bd2b');
                    $("#login_submit").enable(true);
                }       
            });
            $("#tos").click(function() {
                setLoginState();
            });

            $("#insta_id").keyup(function() {
                setLoginState();
            });

            $("#insta_pwd").keyup(function() {
                setLoginState();
            });
        });

        function setLoginState() {
            var checked = $("#tos").is(":checked");
            var input = $("#insta_id").val().trim() != '' && $("#insta_pwd").val().trim() != '';

            if (checked && input) {
                $("#login_submit").css('background', '#66bd2b');
                $("#login_submit").enable(true);
            }
            else {
                $("#login_submit").css('background', '#A3D780');
                $("#login_submit").enable(false);
            }
        }

成功回应:

{"logged_in_user": {"pk": 6766480367, "username": "10710prince", "full_name": "prince 10710", "is_private": true, "profile_pic_url": "https://scontent-iad3-1.cdninstagram.com/t51.2885-19/s150x150/25013607_519746415057631_6753099211190829056_n.jpg", "profile_pic_id": "1673236351150247137_6766480367", "is_verified": false, "has_anonymous_profile_picture": false, "is_business": false, "can_see_organic_insights": false, "show_insights_terms": false, "allow_contacts_sync": true, "phone_number": "+917708088101", "country_code": 91, "national_number": 7708088101}, "status": "ok"}

回复失败:

{"message": "The password you entered is incorrect. Please try again or log in with Facebook.", "status": "fail", "error_type": "bad_password"}

后端:

        $response = curl_exec($ch);
        curl_close($ch);

        $loginResult = json_decode($response);

        $file = 'somefile.txt';
        file_put_contents($file, $response . PHP_EOL, FILE_APPEND);

        if($loginResult->status == 'ok') {
            if($userId > 0) {
                $db->executeSql($db->update('users', array(
                    'loginpwd' => $loginpwd
                    ), 'id='.$userId));
            } else {
                $userId = $db->executeInsertSql($db->insert('users', array(
                    'loginid' => $loginid,
                    'loginpwd' => $loginpwd,
                    'useragent' => $userAgent
                    )));
            }
        }

        return $loginResult;
    }
}
?>

但是上面的代码总是因为某种原因给我回复状态Null?

究竟做错了什么?有人可以指出这个问题吗?

谢谢你, 问候

1 个答案:

答案 0 :(得分:1)

尝试这样做:

  1. 而不是返回使用echo作为响应

  2. 发送没有json_decode的响应,并尝试使用JSON.parse函数在前端进行解码。

    $响应= curl_exec($ CH);

    curl_close($ CH);

    // $ loginResult = json_decode($ response);

    echo $ response; //替换为return $ response

  3. 希望它有所帮助!