How to set content type and POST for backbone.js?

Can I install content-type

and POST

in backbone.js?

this.save(data, {
    success: function (user) {
        callback(user.get('LoginStatus'))
    },

    error: function (user, result, xhr) {

    }
});

      

I get a bad request when I try to make a call to a REST service, it works in fiddler. Do I need to set the content type and type?

Here is the error I am getting

[ERROR][TiHttpClient(  636)] (TiHttpClient-1) [13340,13340] HTTP Error (org.apache.http.client.HttpResponseException): Bad Request
[ERROR][TiHttpClient(  636)] org.apache.http.client.HttpResponseException: Bad Request
[ERROR][TiHttpClient(  636)]    at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:240)
[ERROR][TiHttpClient(  636)]    at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:199)
[ERROR][TiHttpClient(  636)]    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
[ERROR][TiHttpClient(  636)]    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:637)
[ERROR][TiHttpClient(  636)]    at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1217)
[ERROR][TiHttpClient(  636)]    at java.lang.Thread.run(Thread.java:1020)
[ERROR][TiAPI   (  636)]  [REST API] ERROR: " *** FaultException : Object reference not set to an instance of an object."
[ERROR][TiAPI   (  636)]  [REST API] apiCall ERROR: " *** FaultException : Object reference not set to an instance of an object."

      

+3


source to share


2 answers


As labels fetch

and save

wrap the function jQuery.ajax()

, you can set the Content-Type and Request method to Backbone by passing the object a jQuery.ajax()

settings

directly to either fetch

orsave

For example, using the function fetch

:

myModel.fetch({
    type: "POST",
    contentType: "application/json"
});

      

The same with a function save

:



myModel.save({
    type: "POST",
    contentType: "application/json"
});

      

Also I notice that you are providing a data property in your function save

. if you want to pass JSON as POSTed data to a url, you need the following syntax in your function save

:

myModel.save({
    data: JSON.stringify(myObject),
    type: "POST",
    contentType: "application/json"
});

      

+8


source


I don't quite understand the error posted, but if all you want to do is set the content type or change some of the other defaults in the call, then this is entirely possible.

If you look at the function to save the model prototype in Backbone, this.sync or the default Backbone.sync method is actually used to make the call. By checking the Backbone.sync function, you can see that the jQuery ajax method is actually being used to make the call. Note line return $ .ajax (_. Extend (params, options)); hence you should be able to pass anything as parameters to it which the jquery ajax method will use. In the same sync method, you can also see how it sets the default default content type, params.contentType = 'application / json';



You can also write your own sync method for the model and make your own ajax call, changing the default parameters. If your model has its own sync method, it will then be called instead of the default Backbone.sync method.

+1


source







All Articles