简体   繁体   English

更新到 Codeigniter 3.x 后,为什么在 ajax 调用期间出现 json 解析错误?

[英]Why am I getting a json parse error during an ajax call after updating to Codeigniter 3.x?

I am updating my Codeigniter framework from 2.2.6 to 3.0.6.我正在将我的 Codeigniter 框架从2.2.6更新到 3.0.6。 It has broken the existing code that worked before.它破坏了以前工作的现有代码。 Specifically, I am getting the error "SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data" inside the browser console.具体来说,我"SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data" inside the browser console.收到错误"SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data" inside the browser console. I have tried to look and see if this is a known issue when updating, but I have not seen anyone else experiencing this.我试图查看更新时这是否是一个已知问题,但我没有看到其他人遇到此问题。

Here is the javascript that I am using:这是我正在使用的 javascript:

    $('#addServiceItem').on('change', function() {
        var serviceID = $(this).val();
        $.ajax({
            url: '/ajax/get_service_details/' + serviceID,
        }).done(function(data) {
            if (data.status == 'success') {
                addServiceItem(data.service);
            } else {
                alert(data.message);
            }
        });
    });

Also, here is the function that is being called in the ajax url:此外,这是在 ajax url 中调用的函数:

public function get_service_details($serviceID = 0)
    {
        if (!$this->input->is_ajax_request()) {
            exit('No direct script access allowed');
        }

        if ($serviceID == 0) {
            header('Content-type: application/json');
            echo json_encode(array(
                'status' => 'error',
                'service' => null,
                'message' => 'We could not find the service.'
            ));
        }

        $service = $this->services_model->get_service_details($serviceID);

        header('Content-type: application/json');
        echo json_encode(array(
            'status' => 'success',
            'service' => $service,
            'message' => ''
        ));
    }

As stated above, this code worked in the previous version of Codeigniter.如上所述,此代码在早期版本的 Codeigniter 中有效。 As far as I can tell there is an issue with the Ajax call returning a value.据我所知,Ajax 调用返回值存在问题。 In the javascript variable data stays undefined.在 javascript 变量数据中保持未定义。 I assume that there is a syntax standard that has changed.我假设有一个已经改变的语法标准。

If your $serviceID == 0 , then you get error Headers already sent because inside IF you output content and do not terminate code, and outside of IF you again try to set header.如果您的$serviceID == 0 ,那么您会收到错误Headers already sent因为在 IF 内部您输出内容并且不终止代码,并且在 IF 外部您再次尝试设置标头。

Change your code to this one:将您的代码更改为以下代码:

public function get_service_details($serviceID = 0)
{
    if (!$this->input->is_ajax_request()) {
        exit('No direct script access allowed');
    }

    if ($serviceID == 0) {
        header('Content-type: application/json');
        echo json_encode(array(
            'status' => 'error',
            'service' => null,
            'message' => 'We could not find the service.'
        ));

        // Terminate function execution
        return;
    }

    $service = $this->services_model->get_service_details($serviceID);

    header('Content-type: application/json');
    echo json_encode(array(
        'status' => 'success',
        'service' => $service,
        'message' => ''
    ));
}

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

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