Ajax响应在响应页面中显示php文本
这是我的视图文件中的ajax代码Ajax响应在响应页面中显示php文本,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,这是我的视图文件中的ajax代码 $('#bidck').click(function (e) { e.preventDefault(); var data = "ajax=ajax"; $.ajax({ type: 'POST', url: 'http://localhost:8080/test/check', data: data, success: function (data) {
$('#bidck').click(function (e) {
e.preventDefault();
var data = "ajax=ajax";
$.ajax({
type: 'POST',
url: 'http://localhost:8080/test/check',
data: data,
success: function (data) {
console.log(data);
}
});
})
代码运行良好,这方面没有问题,但响应端有问题,这是我的控制器端
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class test extends CI_Controller
{
public function check()
{
$data = array("data" => "true", "m" => "message");
Header('Content-Type: application/json');
echo json_encode($data);
}
}
您不见了
数据类型:“json”
您可以在之后添加这一行
数据:数据
扩展为使用CI_控制器的类必须始终将第一个字母大写
在您的情况下:您需要将test更改为test,并确保文件名相同(test.php)
尝试删除“标题('Content-Type:application/json');”删除了标题('Content-Type:application/json');如果你真的看到你的文件是以开头的吗?不,这是我的第一行,伙计们,我在浏览器中查看了直接链接,它也显示了这样的内容,所以它不是一个jquery问题可能是编码方面的一些问题好的,在echo json_encode之前试试这个,以显示错误,我会将它添加到答案中,它不会显示出来未定义的变量:jsonOUTPUT:
if ($json === false) {
// Avoid echo of empty string (which is invalid JSON), and
// JSONify the error message instead:
$json = json_encode(array("jsonError", json_last_error_msg()));
if ($json === false) {
// This should not happen, but we go all the way now:
$json = '{"jsonError": "unknown"}';
}
// Set HTTP response status code to: 500 - Internal Server Error
http_response_code(500);
}
echo $json;
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
public function check()
{
$data = array("data" => "true", "m" => "message");
Header('Content-Type: application/json');
echo json_encode($data);
}
}
?>