javascript - Why can't I pass a json array as data when doing GET request with jquery ajax? -


if run request this:

 $.ajax({     url: 'http://localhost:8000/api/points/',     contenttype:"application/json",     datatype: "json",     data: json.stringify({"content_type":content_type,"object_id":object_id}),     type: 'get',   }).error(function(r){ $(output).text('error') })   .success(function(r){     $(output).text(r.count);   }) 

its request goes to:

http://localhost:8000/api/points/?{%22content_type%22:8,%22object_id%22:40}

obviously that's bad. works okay if don't json.stringify(), why that?

curiously if post request it's opposite! have stringify data or won't work. why difference?

first of let's fix request:

var req = $.ajax({     method: "get",     url: "http://localhost:8000/api/points/",     datatype: "json", // telling jquery kind of response expect.      data : {id : '12345'} });  req.done(function(data){     // data });  req.fail(function(jqxhr, status, err){     // in case of failure     throw err; }); 

next know dealing :

data : plainobject or string or array data sent server. converted query string, if not string. it's appended url get-requests. by default, data passed in data option object (technically, other string) processed , transformed query string, fitting default content-type "application/x-www-form-urlencoded".

note: the jqxhr.success(), jqxhr.error(), , jqxhr.complete() callbacks deprecated in jquery 1.8. prepare code eventual removal, use jqxhr.done(), jqxhr.fail(), , jqxhr.always() instead.

at last:

no need : json.stringify({"content_type":content_type,"object_id":object_id}) wrong way json.stringify : { 'content_type' : 'type', 'object_id' : 'id' }


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -