2

I have a web page that returns this JSON when called:

[{"id":"53","desc":"Roberts"}]

I am using this jQuery to call it by AJAX:

$.ajax ({
    url: rootPath + "data/topology/stations",
    dataType: 'json',
    data: { areaID: $("#lbxArea").val () },
    success: function (data) {
        // Use data for actions
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert (textStatus);
        alert (errorThrown);
    }
});

I used Firebug to confirm that the data being returned is what I put on top. Despite that, I fall into the error callback and first see parsererror in an alert box, and then I see

SyntaxError: JSON.parse: expected property name or '}'

I tried having the service return

{"list":[{"id":"53","desc":"Roberts"}]}

but that didn't change anything.

Nik
  • 7,113
  • 13
  • 51
  • 80
  • 1
    possible duplicate of [jQuery won't parse my JSON from AJAX query](http://stackoverflow.com/questions/249692/jquery-wont-parse-my-json-from-ajax-query) – sje397 Aug 12 '11 at 13:25
  • 1
    Which version of jQuery are you using? – andyb Aug 12 '11 at 13:27
  • No, @sje397, this does not look like a duplicate of that one at all. In that question, the JSON was clearly malformed (no quotes around property names, etc). The JSON shown here in this question looks fine. – Pointy Aug 12 '11 at 13:29
  • jQuery 1.6.2 (minimized) – Nik Aug 12 '11 at 13:29

3 Answers3

3

Whats the response content-type?! Try testing this response using this:

Getting the response content-type from jQuery.Post

also try not having the dataType: 'json' and check the return!

Community
  • 1
  • 1
Arthur Neves
  • 11,840
  • 8
  • 60
  • 73
2

Well, I've spent some time on this question, but I'll make something that will serve those who have this problem.

The mistake is in wanting a property accesder response coming from PHP, introducing the following message:

*SyntaxError: JSON. parse: expected property name or '}'*

What you should do is convert JSON response to this is to use the function JSON.parse (data); inside we pass the response variable "data". I will comment a bit the code for better understanding:

   success: function (data) {/ / on success ..     
var json = JSON.parse (data);/ / Convert the JSON format input
console.log (JSON.parse (data));/ / explore the answer
  alert ("" + json.msj);/ / Variable Access Testing with alert
....

Everything seems fine up here, however is dimensioned present the error, then that is because the way it is performing the response from PHP.

Here is a practical way to do it right:

We use json_encode function to return the data in JSON format, within the function passed an associative array with the variables that are required, Example:

echo json_encode (array ('success' => true, 'msg' => 'Hello registered user!'));

After these variables acquire client side without any problem, and simply, here a nitrous-code:

$. ajax ({/ / create an AJAX call ...
data: $ (this). serialize (), / / get the form data
type: $ (this). attr ('method'), / / GET or POST
url: $ (this). attr ('action'), / / the file to call

cache: false,
success: function (data) {/ / on success ..

var json = JSON.parse (data);

$ ('# created'). html (json.msj) / / update the DIV

I hope will be helpful ... any questions feel free to ask ...

1

You can install a Firefox add-on "JSONView". Maybe this gives you more information about the JSON string.

If you don't see anything (special JSON markup), you probaby miss a JSON header.


Edit: Firebug 1.4+ should show a JSON tab at a request.

321X
  • 3,153
  • 2
  • 30
  • 42