gpt4 book ai didi

php - 从 php 中的 mvc Controller 方法返回一个 json 值

转载 作者:可可西里 更新时间:2023-10-31 23:46:20 26 4
gpt4 key购买 nike

我想从 mvc Controller 方法中获取一个 json 类型的值。一切正确,但发生错误。

我的 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));
});
}

这是我的 php 代码(mvc Controller 方法):

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

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

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

我该如何解决这个问题?谢谢...

最佳答案

尝试使用

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

关于php - 从 php 中的 mvc Controller 方法返回一个 json 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34866975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com