How does Javascript differentiate between Json Objects and String? -
i reading code snippet , figuring out how works when weird javascript problem came up.
in javascript called
var apiurl = '/api/v1/pin/?format=json&order_by=-id&offset='+string(offset); ... $.get(apiurl, function(pins) { (...; < pins.objects.length; i++) ... // works fine });
their api returns json format:
{"meta": {"limit": 50, "next": "?limit=50&format=json&order_by=-id&offset=60", "offset": 10, "previous": null, "total_count": 79}, "objects": [ {...},{...}, ... ]}
i tried mimic $.get never accepted simplified json string.
// attempt $.get(myapiurl, function(pins) { (...; < pins.objects.length; i++) ... // error: undefined length });
my json string in similar shortened format.
{"objects": [{...},{...}, ... ]}
i couldn't work until googled $.getjson() command. wondering why javascript code works $.get whereas mine has use $.getjson??
is kind of header can set force javascript read json?
if don't specify content type explicitly jquery try determine data type based on headers returned server.
if not using json serializers, verify validity of json using http://jsonlint.com/ or this, otherwise error.
correct contenttype header json is: application/json
Comments
Post a Comment