VikR
VikR

Reputation: 5142

jQuery .ajax Call Returns JSON ParseError Even Thought JSON Appears to be Correct?

I'm getting a ParseError even though my JSON validates on jsonlint.com.

Here is the jQuery code:

$.ajax({
    url: path,
    type: 'GET',
    data: {},
    cache: false,
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',                  
    success: function (data) {
        var a = 'breakpoint here doesn't activate';
    },
    error: function (x, y, z) {
        var b = 'code execution stops at a breakpoint here.';
    }
});

Here is the PHP code that is being called:

function getAllAnswersToHitViaAjax($theJobName) {
    $testData[0] = 'testing123';
    $encodedData = json_encode($testData);
    echo $encodedData;
    return;
}

This comes back to a breakpoint set in the error: function of my .ajax call. Parameter Y is set to "parseerror", and x.responseText =

["testing123"] 

I've been looking into this for hours so far. I've looked at many relevant StackOverflow posts, but none have solutions that work in this case.

How can I get a success response from this .ajax call?

Thanks very much in advance to all for any info.

Upvotes: 2

Views: 759

Answers (6)

user3040610
user3040610

Reputation: 760

Try with the below code,

    function getAllAnswersToHitViaAjax($theJobName) {
    $testData[0] = 'testing123';
    echo json_encode($testData);
    die();
    }

    $.ajax({
    url: path,
    type: 'GET',
    dataType: 'json'
    success: function(data){
        console.log(data);
        var result = JSON.parse(data);
        console.log(result);
    }
    });

Upvotes: 1

Vineet
Vineet

Reputation: 4645

Please use

 $.ajax({
    url: path,
    type: 'GET',
    cache: false,
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',                  
    success: function (data) {
        var a = 'breakpoint here doesn\'t activate';
    },
    error: function (x, y, z) {
        var b = 'code execution stops at a breakpoint here.';
    }
});

and in your PHP CODE Paste this code.

function getAllAnswersToHitViaAjax($theJobName) {
    $testData[0] = 'testing123';
    $encodedData = $testData;
    echo json_encode($encodedData);
    exit;
}

May be after digging more in your code I assume that in your PHP code you were not passing data properly. Please use exit or die method to pass data back to ajax. Please check.

Upvotes: 1

radiaph
radiaph

Reputation: 4101

There's nothing visibly wrong with your code, and it works fine when I try it on my local machine. However, your comment above is a big clue:

I just looked at the z param in the Safari console, and found this:"undefined is not a function (evaluating 'JSON.parse(a+"")')" How could that be happening?

It could happen if some code somewhere uses "JSON" as a global variable name, hiding the built-in window.JSON object.

Upvotes: 3

Matarishvan
Matarishvan

Reputation: 2432

Just Use

function getAllAnswersToHitViaAjax($theJobName) {
   $testData[0] = 'testing123';
   echo json_encode($testData);
}

Then in your AJAX you can do

$.ajax({
 url: path,
 type: 'GET',
 dataType: 'json'
 success: function(data){
    for (var i = 0; i < data.length; i++) {
        //DO YOUR STUFF          
    }
 }
});

Upvotes: 1

brianary
brianary

Reputation: 9332

JavaScript allows either single or double quotes for strings, but JSON only allows double quotes. See http://www.json.org/

See also jQuery.parseJSON single quote vs double quote

Upvotes: 1

Mayur
Mayur

Reputation: 116

Check for Notice or Warning in your php code, if their is any then remove that and then try. Hop this will help you.

Upvotes: 1

Related Questions