简体   繁体   English

ajaxform 给我错误的回应

[英]ajaxform giving me wrong response

I am having below form that login to instagram depends on the response, if it loggin means "ok", then success, if not it should giving me else state.我在下面的表单中登录到 Instagram 取决于响应,如果它登录意味着“ok”,那么成功,如果不是它应该给我其他状态。

but in my case below it always giving me null state for some reasons.但在我下面的情况下,由于某些原因,它总是给我空状态。

i've tried to play around it with failed attempt.我试图以失败的尝试来解决它。

$(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);
            }
        }

Success response:成功回复:

{"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"}

Failed response:失败回复:

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

Backend:后端:

        $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;
    }
}
?>

But my code above always giving me response state Null for some reason?但是由于某种原因,我上面的代码总是给我响应状态 Null ?

what am doing wrong exactly?, can somebody point to the bug, issue?到底做错了什么?,有人可以指出错误,问题吗?

thank you, regards谢谢,问候

Try doing these :尝试做这些:

  1. instead of return use echo for the response而不是 return 使用 echo 作为响应

  2. send the response without json_decode and try the decoding on frontend using JSON.parse function.发送不带 json_decode 的响应并尝试使用 JSON.parse 函数在前端解码。

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

    curl_close($ch); curl_close($ch);

    //$loginResult = json_decode($response); //$loginResult = json_decode($response);

    echo $response;回声 $response; //replaced with return $response // 替换为 return $response

Hope it helps !希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM