javascript - Backbone sends request payload instead of FormData -
i trying post info server using backbone collection's create method this:
this.collection.create(modeldata, { wait:true, contenttype:"application/x-www-form-urlencoded" }); here model info contains formatted json object. problem post request gets cancelled. info sent in form of requestpayload.
using rest client able post because sends info using formdata instead of requestpayload.
any takers?
edit
this in network console:
request url:http://myendpointurlhere.com/ request headers caution: provisional headers shown. accept:application/json, text/javascript, */*; q=0.01 cache-control:no-cache content-type:application/x-www-form-urlencoded origin:http://localhost:8888 pragma:no-cache referer:http://localhost:8888/case/12345 user-agent:mozilla/5.0 (windows nt 6.2; wow64) applewebkit/537.36 (khtml, gecko) chrome/35.0.1916.153 safari/537.36 form dataview sourceview url encoded model:{"startdate":"19/5/2014","acute":"true","terminal":"true","comment":"","name":"hypothyroidism","caseid":"4872585904914432"} edit 2
for reason worked me. allow me know thoughts on this:
this.collection.create({}, { data:modeldata, wait:true, emulatejson:true });
if need request sent formdata should utilize emulatejson:true options request. from docs:
if you're working legacy web server can't handle requests encoded application/json, setting backbone.emulatejson = true; cause json serialized under model parameter, , request made application/x-www-form-urlencoded mime type, if html form.
this.collection.create(modeldata, { wait:true, emulatejson:true }); or, if requests need utilize emulatejson can set globally backbone.emulatejson=true;
update:
if need send formdata object server, need modify sync models/collection generate before send server. here's can seek (not tested):
var yourmodel = backbone.model.extend({ sync: function(method, model, options){ options = options || {}; var formdata = new formdata(); var json = model.tojson(); _.each(json, function(value, key){ formdata.append(key, value); }); options.data = formdata; options.emulatejson = true; // of import because sending formdata homecoming backbone.model.prototype.sync.call(this, method, model, options); } }); http://mrjoelkemp.com/2013/09/backbone-js-model-save-without-the-emulatejson-model-attribute/ provides illustration of how works.
javascript jquery http rest backbone.js
No comments:
Post a Comment