Home > Mobile >  I'm trying to parse json response of a php api in ajax call but getting bad escape character er
I'm trying to parse json response of a php api in ajax call but getting bad escape character er

Time:11-17

This error only occurs when I try to parse large JSON response, suppose 100 records. I'm trying to parse JSON containing HINDI characters in response. I've tried validating the JSON response but no online tool shows any error in the response that I'm getting.

Error

Uncaught SyntaxError: JSON.parse: bad escaped character at line 1 column 65529 of the JSON data

AJAX Call

$.ajax(
            {
                url: "api/getPrintReport.php",
                type: "POST",
                data: {
                    fromDateLimit: dateVal,
                    toDateLimit: dateVal1,
                    reportType: selectedReportIndex,
                    cadreCode: cadreCode,
                    pageNo: startPageNo
                },
                success: function (result) {
                    //console.log(result);
                    const json = JSON.parse(result);
                    if (json['status'] === "1")
                    {
                        // doing something...
                    }
                    else {
                        modalBox.modal('show');
                        modalBoxMessage.html(json['message']);
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    modalBox.modal('show');
                    modalBoxMessage.html(errorThrown);
                }
            });

When i try to debug the code, the debugger never goes ahead of JSON.parse() method.

JSON Response

{
"message": "Data retrieved successfully.",
"status": "1",
"currentPage": "1",
"totalPages": "4",
"data": [
    {
      // Around 10-15 key-value pairs
    },
    {}...more JSON objects here
]}

I've validated the JSON response with the help of some online tools but can't find any bad escape character or error in it. I'm only getting this error when I try to get more than 80 or 100 records. The code works perfectly when I parse lesser records. I'm using PostgreSQL as DB.

I even tried searching if there's a limit set for a response but I came to know that there's no limit until we set one, but in my case, I haven't set any limit. Please help me.

Screenshot of Network Tool

Here in the 1st line i'm getting a garbage/null character at the end.

the same character continues in the start of the 2nd line.

Here in the 1st line, I'm getting a garbage/null character at the end. The same character continues at the start of the 2nd line.

CodePudding user response:

Instead of trying to parse the json... try this

$.ajax(
            {
                url: "api/getPrintReport.php",
                type: "POST",
                dataType: 'json', //<---------- TELL THE SERVER YOU WANT JSON
                data: {
                    fromDateLimit: dateVal,
                    toDateLimit: dateVal1,
                    reportType: selectedReportIndex,
                    cadreCode: cadreCode,
                    pageNo: startPageNo
                },
                success: function (result) {
                    //console.log(result);
                    //const json = JSON.parse(result);
                    if (result.status === "1")
                    {
                        // doing something...
                    }
                    else {
                        modalBox.modal('show');
                        modalBoxMessage.html(json['message']);
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    modalBox.modal('show');
                    modalBoxMessage.html(errorThrown);
                }
            });
  • Related