javascript 如何为backbone.js 设置内容类型和POST?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14730848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:26:24  来源:igfitidea点击:

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

javascriptbackbone.js

提问by theJava

Can I set content-typeand POSTin backbone.js?

我可以在backbone.js中设置content-typePOST吗?

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

    error: function (user, result, xhr) {

    }
});

I am getting bad-request when I try to make a REST service call, it works in fiddler. Do we need to set the type and Content-type?

当我尝试进行 REST 服务调用时收到错误请求,它在 fiddler 中工作。需要设置type和Content-type吗?

Here is the error i get

这是我得到的错误

[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."

回答by dcarson

As the backbone fetchand savemethods wrap the jQuery.ajax()function you can set the Content-Type and Request Method in Backbone by passing in the a jQuery.ajax()settingsobject directly to either the fetchor savefunction

由于主干fetchsave方法包装了jQuery.ajax()函数,您可以通过将 ajQuery.ajax()settings对象直接传递给fetchorsave函数来设置主干中的内容类型和请求方法

For example using the fetchfunction:

例如使用fetch函数:

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

Its the same using the savefunction:

使用save函数也是一样的:

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

Also I notice you are providing a data property in your savefunction. if you want to pass JSON as POSTed data to a URL, you'll need the following syntax in your savefunction:

我还注意到您在save函数中提供了一个数据属性。如果要将 JSON 作为 POSTed 数据传递到 URL,则在save函数中需要以下语法:

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

回答by PJR

I do not quite understand the error posted but if all you want to do is set the content-type or alter some other default settings in the call, then its quite possible.

我不太明白发布的错误,但如果您只想设置内容类型或更改呼叫中的其他一些默认设置,那么很有可能。

If you take a look at the save function for the Model prototype in Backbone, it actually is using this.sync or the default 'Backbone.sync' method to make the call. Checking the Backbone.sync function, you can see that it is actually using the jquery's 'ajax' method to make the call. Note line return $.ajax(_.extend(params, options)); hence you should be able to pass anything as options to it that the jquery's ajax method would take. In the same sync method you can also see you how it is setting the standard default content type, params.contentType = 'application/json';

如果您查看 Backbone 中模型原型的保存函数,它实际上是使用 this.sync 或默认的“Backbone.sync”方法进行调用。查看Backbone.sync 函数,可以看到它实际上是使用jquery 的'ajax' 方法进行调用的。注意行 return $.ajax(_.extend(params, options)); 因此,您应该能够将任何内容作为 jquery 的 ajax 方法将采用的选项传递给它。在相同的同步方法中,您还可以看到它是如何设置标准默认内容类型的,params.contentType = 'application/json';

You could 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 would then be called instead of the default Backbone.sync method.

您还可以为模型编写自己的同步方法,并进行自己的 ajax 调用以更改默认参数。如果您的模型有自己的同步方法,那么它将被调用而不是默认的 Backbone.sync 方法。