【问题标题】:Ajax request in cordova not working科尔多瓦的 Ajax 请求不起作用
【发布时间】:2017-10-20 09:22:50
【问题描述】:

我正在对 php 脚本使用 ajax 请求来注册新用户,它可以工作并将其保存在数据库中,但返回错误而不是成功。

这是我的 ajax 请求:

$.ajax({
                    type: "POST",
                    url: "http://localhost/webAPI/register.php",
                    data: dataString,
                    crossDomain: true,
                    cache: false,                        
                    success: function (data) {
                        if (data == 1)
                            alert("success");

                        else(data == 0 ) 
                            alert("error");

                    },
                    error: function (){
                           alert("An Error Ocurred");
                     }


                });

这是我的 php 脚本:

if($_POST)
{
    $user_name      = $_POST['username'];
    $user_password  = $_POST['password'];
    $joining_date   = date('Y-m-d H:i:s');

    //password_hash see : http://www.php.net/manual/en/function.password-hash.php
    $password   = password_hash( $user_password, PASSWORD_BCRYPT, array('cost' => 11));


        //if($count==0){
            $stmt = $db_con->prepare("INSERT INTO users(username,password,joiningdate) VALUES(:uname,:pass,:jdate)");
            $stmt->bindParam(":uname",$user_name);
            $stmt->bindParam(":pass",$user_password);
            $stmt->bindParam(":jdate",$joining_date);

            if($stmt->execute())
            {
                echo 1;

            }
            else
            {
                echo 0;

            }

}

【问题讨论】:

  • 您收到什么警报? “错误”还是“发生错误”?
  • 在 Ajax 函数中对数据对象进行 json 字符串化
  • 发生错误
  • 好吧,看我的回答,看error函数,它一定会告诉你更多关于错误的信息。
  • 问题是,它必须在 ajax 请求中包含 datatype: 'json' 。就是这样!

标签: php ajax cordova


【解决方案1】:

将 else(data == 0 ) 替换为 else

 $.ajax({
                        type: "POST",
                        url: "http://localhost/webAPI/register.php",
                        data: dataString,
                        crossDomain: true,
                        cache: false,                        
                        success: function (data) {
                            if (data == 1)
                                alert("success");

                            else
                                alert("error");

                        },
                        error: function (){
                               alert("An Error Ocurred");
                         }




  });

在 PHP 代码中

if($stmt->execute())
            {
                exit(1);

            }
            else
            {
                exit(0);

            }

【讨论】:

  • 我试过了,它仍然给我警报 发生错误,很奇怪,因为用户名和密码正在保存在数据库中,我也在 php 中尝试过这个 echo json_encode("success");跨度>
  • 这没有帮助。 if 条件永远不会执行
  • 尝试退出你的php代码可能是它没有返回代码
  • 唯一的问题是它必须在 ajax 请求中包含 datatype 属性。
【解决方案2】:

您可以解析 JSON 格式的 PHP 响应以检查来自 JS 的结果。

PHP 响应:

if ($stmt->execute()) {
    die(json_encode(['return' => true]));
} else {
    die(json_encode(['return' => false]));
}

在 JS 中,只需检查 return 标志:

$.ajax({
    type: "POST",
    url: "http://localhost/webAPI/register.php",
    data: dataString,
    dataType: 'JSON', // tell JS that the PHP response is json formated
    crossDomain: true,
    cache: false,                        
    success: function (data) {
        if (data.return) { // check if return is true
            alert("success");
        } else { // if return is false
            alert("error");
        }
    },
    error: function (jqXHR, textStatus, errorThrown){
        console.log(textStatus, errorThrown); // this will tell you more in case of unsuccessful request
    }
});

希望对你有帮助。

编辑:

查看Ajax error 属性函数。它必须告诉你更多关于错误的信息。

重新编辑:

您发送给 PHP 的 dataString 必须是 JSON 对象。所以从你的评论来看,它一定是这样的:

var dataString = {
    username: $("#username").val(),
    password: $("#password").val()
};

【讨论】:

【解决方案3】:

唯一的问题是 ajax 请求中缺少数据类型属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2017-04-26
    • 1970-01-01
    • 2017-09-20
    • 2016-08-18
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多