从php中的mvc控制器方法返回一个json值

时间:2020-12-31 11:24:22

i would like to get an json type value from an mvc controller method. everything is correct but an error occures'.

我想从mvc控制器方法获取json类型值。一切都是正确的,但发生错误'。

my jquery ajax function:

我的jquery ajax函数:

function user_login(uname,pass){
    $.ajax({
        url: 'http://localhost/s/login_request',
        type:'POST',
        data:{uname:uname,pass:pass},
        dataType:"json",
        cache: false,
    })
    .done(function(response){
         //do something   
         alert('1234');
    })
    .fail(function(jqXHR,textStatus){
        alert(JSON.stringify(jqXHR));
    });
}

and here is my php code(mvc controller method):

这是我的PHP代码(mvc控制器方法):

function login_request(){
        header('Content-Type: application/json');
        echo json_encode(array('testvalue'));   
}

when i run the code, the .fail section executed and the following value was returned:

当我运行代码时,执行.fail部分并返回以下值:

{"readyState":4,"responseText":"[\"testvalue\"]","status":200,"statusText":"OK"}

how can i solve this? thanks...

我怎么能解决这个问题?谢谢...

1 个解决方案

#1


0  

Try using

$.ajax({
    url: 'http://localhost/s/login_request',
    type:'POST',
    data:{uname:uname,pass:pass},
    dataType:"json",
    cache: false,
    success: function(data) { },
    fail: function(xhr, status) { alert(JSON.stringify(xhr)) },
    error: function() { },
    complete: function() { alert('1234') }
});

#1


0  

Try using

$.ajax({
    url: 'http://localhost/s/login_request',
    type:'POST',
    data:{uname:uname,pass:pass},
    dataType:"json",
    cache: false,
    success: function(data) { },
    fail: function(xhr, status) { alert(JSON.stringify(xhr)) },
    error: function() { },
    complete: function() { alert('1234') }
});