簡體   English   中英

無法從ajax請求中的php獲得返回

[英]Unable to get the return from php in ajax request

我只想從ajax數據帖子中獲取一些返回值。 我不確定為什么我沒有獲得成功。 請查看代碼,並告訴我我錯了

我的jQuery代碼

$("#btnlogin").click(function(){

    var email = $("#emaillog").val();
    var password = $("#passlog").val();
    console.log('test');
    /* $.ajax({
        url: 'home2/login_user',
        //data: {'title': title},  change this to send js object
        type: "post", 
        data: 'email=' + email+'&password='+password,
        success: function(output) {
            url:"home2/login_user",
            data: 'email=' + email+'&password='+password,
            alert(output);
        }
    });*/

    $.ajax ({
         url:"home2/login_user",
         type: "post",
         dataType: 'json', 
         data: 'email=' + email+'&password='+password,
         success: function (data) {
             $.each(data, function(key, value) {
                 console.log(key,'--',value);
             });
              //iterate here the object
         }
     });
}); 

我的PHP代碼

public function Login_user()
    {
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $data['result'] = $this->Home_model->login_to_user($email,$password); 
        echo json_encode ($data['result']);     


    }

在php代碼中,我在jQuery中回顯結果。 我沒有取得任何成功的結果

謝謝

使用parseJSON

var obj = jQuery.parseJSON(data);
console.log(obj.key);

原因是因為您的后端代碼似乎無法找到用戶名和密碼參數。 您在以下代碼行中以錯誤的方式傳遞了它們:

data: 'email=' + email+'&password='+password,

將字符串替換為JavaScript對象:

data: { email: email, password: password }

好吧,首先,您傳遞的參數就像是一個get請求,而不是它。

更改傳遞給類似內容的方式。

 $.ajax ({
         url:"home2/login_user",
         type: "post",
         dataType: 'json', 
         data: { field1: "hello", field2 : "hello2"},
         success: function (data) {
             $.each(data, function(key, value) {
                 console.log(key,'--',value);
             });
              //iterate here the object
         }
     });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM