简体   繁体   English

Ajax请求总是出错

[英]Ajax request always get error

Actually i'm using CodeIgniter as backend in my project and jQuery 2.1.3 in my frontend. 实际上,我在项目中使用CodeIgniter作为后端,在前端中使用jQuery 2.1.3。

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

When i run this function: 当我运行此功能时:

var side = 'biltyveri';
var request_checkUrl = '/antibot/antibot_data?script=' + side;

$.ajax({
    url: request_checkUrl,
    type: "GET",
    dataType: 'json',
    beforeSend: function () {
        document.title = "Running...";
    },
    success: function (data) {
        document.title = "Success.";
        alert("success");
    },
    error: function (data) {
        alert("ERROR");
        console.log(data);
    }
});

Always displays ERROR and never displays the success function. 始终显示错误,从不显示成功功能。 I don't know what i'm doing wrong. 我不知道我在做什么错。

In my Controller i'm running this code: 在我的控制器中,我正在运行以下代码:

echo $this->framework->ajaxJSONResponse(200, $data);

And this is the function it gives a response json: 这是它给出响应json的函数:

function ajaxJSONResponse($status, $data) {
    header('Content-type: application/json');
    $response             = array();
    $response['status']   = $status;
    $response['data']     = $data;
    return json_encode($response);
}

The code above give a json response as follows: 上面的代码给出了json响应,如下所示:

{
    "status": 200,
    "data": {
        "text": "en TV",
        "images": [
            {
                "hash": "47a32df0c4b1f0b522e5faf35a46aacd95fe0ed4",
                "file": "ABImage_plane_1"
            },
            {
                "hash": "e11f83f4411364546329c8a8bf88da0dffd27029",
                "file": "ABImage_house_2"
            },
            {
                "hash": "93b4454ac09e7d7478fa2d25322e0e784370ea7a",
                "file": "ABImage_car_5"
            },
            {
                "hash": "36fac21a830b922edb507487d833556aeb9688f7",
                "file": "ABImage_clock_4"
            },
            {
                "hash": "cd1df47e052a5d0d50dab61b3e716339be0c6e68",
                "file": "ABImage_TV_3"
            },
            {
                "hash": "59e7f70b7874a500e576e25077adf254c52f5ee8",
                "file": "ABImage_train_4"
            }
        ]
    }
}

Thank you very much in advance. 提前非常感谢您。

EDIT: route: 编辑:路线:

$route['antibot/antibot_data'] = "antibot/antibot_interface_controller/antibot_data";

Background: ajax throwing error with right outcome 背景: ajax投掷错误,结果正确

If you using Codeigniter, Your URL is wrong 如果您使用Codeigniter,则您的URL错误

var side = 'biltyveri';
var request_checkUrl = '/antibot/antibot_data?script=' + side;

This should come as 这应该是

url: "<?php echo base_url()?>Controller_name/method_name",

and method should be post 和方法应该post

type: "POST",

and pass data with 并传递数据

data : {"script : side, someOther: anotherValue"}

in controller method, Use this 在控制器方法中,使用此

$script = $_POST['script'];
$someOther = $_POST['anotherValue'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM