【问题标题】:Codeigniter not receive value from ajaxCodeigniter 不接收来自 ajax 的值
【发布时间】:2015-10-23 04:55:08
【问题描述】:

我真的需要一些帮助。我正在使用 phonegap/cordova 开发移动应用程序,并使用 codeigniter 开发网络服务。这个应用程序具有注册功能,这意味着应用程序将通过 Ajax 将数据发送到 codeigniter 控制器,然后将其保存到数据库中。实际上它在本地主机中工作正常。但是在我将所有 codeigniter 文件传输到实时服务器后,ajax 返回错误。我已经在 codeigniter 中设置了 config、database、base_url。有人知道是什么问题吗?

这里是ajax代码

var base_url = "http://bizprohandler.com/token/";

var reg = {"name": name, "email": email, "country": country, "mobile_number": mobile_number, "bitcoin_id": bitcoin_id, "user_id": user_id, "password": password};
$.ajax({
        type       : "POST",
        url        : base_url+'index.php/webservices/register',
        data       : reg,
        dataType   : 'json',
        timeout    : 2000,
        success: function(e){
            if(e.status == 0){
                $.mobile.changePage("#login",{reverse:false,transition:"slide"});
                alert(e.message);
            }
        },
        error: function(e){
            alert('error');
        }
    }); 

这是codeigniter控制器

public function register()
{
    $json = "";
    $result_array = array();

    $json = file_get_contents('php://input');

    $data = json_decode($json, true); //decode into array.\

    $name             = $data['name'];
    $email        = $data['email'];
    $country          = $data['country'];
    $mobile_number   = $data['mobile_number'];
    $bitcoin_id       = $data['bitcoin_id'];
    $user_id          = $data['user_id'];
    $password         = md5($data['password']);

    $this->user_model->insert_user($name,$email,$country,$mobile_number,$bitcoin_id,$user_id,$password);
    $result_array['status'] = 0;
    $result_array['message'] = 'Successfully registered';
    $rep = json_encode($result_array);
    echo $rep;
    exit;
}

【问题讨论】:

  • alert(e) 成功给予了什么??
  • 在 localhost 中,如果我执行 JSON.stringify(e),它会提醒 {"status": "0", "message": "Successfully registered"}。但在实时服务器中,ajax 会提示错误。
  • 出现什么错误??
  • 我警告('错误')。因此,在尝试与实时服务器通信时,这意味着 ajax 不会成功。
  • @SyazwiZaili 在你的 ajax 中,尝试error: function (jqXHR, textStatus, errorThrown) { alert(jqXHR); alert(textStatus); alert(errorThrown); } 来查看错误。

标签: ajax codeigniter cordova


【解决方案1】:

这可能不是它失败原因的答案,但它可以帮助您查看返回的错误。我为register() 提出的代码更像是CodeIgniter-ish。

function register()
{
  // returns array of all POST items passed through the XSS filter
  $data = $this->input->post(NULL, TRUE); 

  $this->user_model->insert_user($data);//you might need to modify insert_user()?
  $result_array['status'] = 0;
  $result_array['message'] = 'Successfully registered';
  echo json_encode($result_array);
}

正如代码注释所述,您可能需要修改user_model->insert_user()。由于您没有共享该代码,因此不知道那里发生了什么。除非您对插入有特殊要求,否则可以直接插入:$this->db->insert('yourTable', $data);

修改了以下 AJAX 代码,以便error: function... 会提醒有用的故障排除信息。

var base_url = "http://bizprohandler.com/token/";
var reg = {"name": name, "email": email, "country": country, "mobile_number": mobile_number, "bitcoin_id": bitcoin_id, "user_id": user_id, "password": password};

$.ajax({
        type       : "POST",
        url        : base_url+'index.php/webservices/register',
        data       : reg,
        dataType   : 'json',
        timeout    : 2000,
        success: function(e){
            if(e.status == 0){
                $.mobile.changePage("#login",{reverse:false,transition:"slide"});
                alert(e.message);
            }
        },
        error: function(jqXHr, textStatus, errorThrown){
            alert(textStatus);
            alert(errorThrown);
        }
    }); 

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多