Or any other function to preprocess your data for that matter :)
或者任何其他的函数来预先处理你的数据:)
Because of my server side framework, I always need to call JSON.stringify before sending my data - unnecessary boilerplate, that you can forget to add.
由于我的服务器端框架,我总是需要调用JSON。在发送我的数据之前stringify -不必要的样板文件,你可以忘记添加。
Right now I have:
现在我有:
$.ajax({
[...]
data: JSON.stringify({ someData: self.someData }),
[...]
});
I would prefer:
我希望:
$.ajax({
[...]
data: { someData: self.someData },
[...]
});
I've looked into ajaxSetup, but can't find a solution for this, so far...
我已经研究了ajaxSetup,但是到目前为止还没有找到解决方案……
Update
更新
For a reason why I need this, see the following this question. I could fix this on the serverside, but for now I'm looking for a way to fix this on the clientside.
出于我需要这个的原因,请参见下面这个问题。我可以在服务器端修复它,但是现在我正在寻找在客户端修复它的方法。
4 个解决方案
#1
25
No, there is no built-in way to pre-process your data from an object to JSON. However, you can use ajaxSetup
and a beforeSend
to do it for you.
不,没有内置的方法可以将数据从对象预先处理到JSON。但是,您可以使用ajaxSetup和前面的beend为您完成它。
$.ajaxSetup({
beforeSend: function(jqXHR,options){
if ( options.contentType == "application/json" && typeof options.data != "string" ) {
options.data = JSON.stringify(options.data);
}
}
});
Now just make sure to set your contentType
to application/json
on requests that need to send json to the server so that it will get caught by the if statement.
现在,只要确保在需要将json发送到服务器的请求中将内容类型设置为application/json,这样它就会被if语句捕获。
#2
1
Here an alternative approach that uses a jQuery.prefilter:
这里有一种使用jQuery.prefilter的替代方法:
$.ajaxPrefilter("json", function(options, originalOptions) {
options.data = JSON.stringify(originalOptions.data || null);
options.contentType = "application/json" // content type of *request*
});
$.ajax({
data: {foo: [1,2,3]},
dataType: "json" // expected content type of *response* (must match prefilter, above!)
[...]
});
Because prefilters match on the dataType
option, we have to set it manually in our $.ajax
request. If the dataType
matches the prefilter ("json") then before the request is sent, it will convert the data
object to a string, and set the contentType
header to match ("application/json").
因为预过滤器与dataType选项匹配,所以我们必须在我们的$中手动设置它。ajax请求。如果数据类型匹配预过滤器(“json”),那么在发送请求之前,它将把数据对象转换为字符串,并将内容类型标头设置为匹配(“application/json”)。
Keep in mind that this a a global change that will impact ALL future $.ajax
requests with dataType: "json"
!
请记住,这是一个全球性的变化,将影响所有未来的美元。使用数据类型的ajax请求:“json”!
#3
0
Here's the jQuery.prefilter function i use (it's better than the beforeSend approach), it will match any datatype, and will serialize any object in a post or put request.
这是jQuery。我使用的预过滤函数(它比前面的方法好),它将匹配任何数据类型,并在post或put请求中序列化任何对象。
$.ajaxPrefilter(function (options, org) {
var rtype = options.type.toLowerCase();
if ((rtype === "post" || rtype === "put") && org.data !== null && typeof org.data === 'object') {
options.data = JSON.stringify(org.data);
}
});
hope this helps.
希望这个有帮助。
#4
-3
User Jquery.getJSON().You can directly get json data.
用户Jquery.getJSON()。可以直接获得json数据。
$.getJSON('', function(data) {
//you can use data.
});
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.getJSON/
#1
25
No, there is no built-in way to pre-process your data from an object to JSON. However, you can use ajaxSetup
and a beforeSend
to do it for you.
不,没有内置的方法可以将数据从对象预先处理到JSON。但是,您可以使用ajaxSetup和前面的beend为您完成它。
$.ajaxSetup({
beforeSend: function(jqXHR,options){
if ( options.contentType == "application/json" && typeof options.data != "string" ) {
options.data = JSON.stringify(options.data);
}
}
});
Now just make sure to set your contentType
to application/json
on requests that need to send json to the server so that it will get caught by the if statement.
现在,只要确保在需要将json发送到服务器的请求中将内容类型设置为application/json,这样它就会被if语句捕获。
#2
1
Here an alternative approach that uses a jQuery.prefilter:
这里有一种使用jQuery.prefilter的替代方法:
$.ajaxPrefilter("json", function(options, originalOptions) {
options.data = JSON.stringify(originalOptions.data || null);
options.contentType = "application/json" // content type of *request*
});
$.ajax({
data: {foo: [1,2,3]},
dataType: "json" // expected content type of *response* (must match prefilter, above!)
[...]
});
Because prefilters match on the dataType
option, we have to set it manually in our $.ajax
request. If the dataType
matches the prefilter ("json") then before the request is sent, it will convert the data
object to a string, and set the contentType
header to match ("application/json").
因为预过滤器与dataType选项匹配,所以我们必须在我们的$中手动设置它。ajax请求。如果数据类型匹配预过滤器(“json”),那么在发送请求之前,它将把数据对象转换为字符串,并将内容类型标头设置为匹配(“application/json”)。
Keep in mind that this a a global change that will impact ALL future $.ajax
requests with dataType: "json"
!
请记住,这是一个全球性的变化,将影响所有未来的美元。使用数据类型的ajax请求:“json”!
#3
0
Here's the jQuery.prefilter function i use (it's better than the beforeSend approach), it will match any datatype, and will serialize any object in a post or put request.
这是jQuery。我使用的预过滤函数(它比前面的方法好),它将匹配任何数据类型,并在post或put请求中序列化任何对象。
$.ajaxPrefilter(function (options, org) {
var rtype = options.type.toLowerCase();
if ((rtype === "post" || rtype === "put") && org.data !== null && typeof org.data === 'object') {
options.data = JSON.stringify(org.data);
}
});
hope this helps.
希望这个有帮助。
#4
-3
User Jquery.getJSON().You can directly get json data.
用户Jquery.getJSON()。可以直接获得json数据。
$.getJSON('', function(data) {
//you can use data.
});
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.getJSON/