My JSON string looks like this:
我的JSON字符串如下所示:
{
"main_object":{
"id":"new",
"formData":"language=nl_NL&getExerciseTitle=3213&question_takeAudio_exerciseWord%5B0%5D=3213&Syllablescounter%5B0%5D=3213S&Syllablescounter%5B1%5D=321"
}
}
As you can see I have language=nl_NL
, but I am looking to have it like this -> "language": "nl_NL
and so on, instead of the &
in between.
正如您看到的,我有language=nl_NL,但是我希望有这样的->“language”:“nl_NL等等,而不是& in between。
This is most likely what is causing the long string ->
这很可能是导致长字符串>的原因
function saveExerciseAjaxCall() {
$("#my_form").on("submit", function(event) {
event.preventDefault();
$.ajax({
url: 'saveJson.php',
type: 'POST',
data: {
id: getUrlParameter('id'),
formData: $('#my_form').serialize()
},
dataType: 'json',
}).done(function(response) {
});
});
}
The part where it is being serialize()
, but to be honest: I have no idea what to do to change this, I read something about serializeArray()
but that will display it in my JSON file like this -> "name": "exampleFor*": "value": "TheValueGiven"
something that looks like this. So does anyone have a solution on how to change my AJAX so it won't end up being one long string? But for it to be like "language": "nl_NL"
etc.?
序列化()的部分,但是老实说:我不知道要做什么来改变它,我读了一些关于serializeArray()的内容,但是它会在我的JSON文件中显示出来——>“name”:“exampleFor*”:“value”:“TheValueGiven”,看起来是这样的。有没有人知道如何改变我的AJAX所以它不会变成一个长字符串?但是要像“语言”:“nl_NL”等等?
1 个解决方案
#1
0
You can convert your urlParameter format to Object like this :
您可以将您的urlParameter格式转换为如下对象:
var obj = {
"id": "new",
"formData": "language=nl_NL&getExerciseTitle=3213&question_takeAudio_exerciseWord%5B0%5D=3213&Syllablescounter%5B0%5D=3213S&Syllablescounter%5B1%5D=321"
};
obj.formData = JSON.parse('{"' + decodeURI(obj.formData).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
console.log(obj.formData);
In your code :
在你的代码:
function saveExerciseAjaxCall() {
$("#my_form").on("submit", function(event) {
event.preventDefault();
$.ajax({
url: 'saveJson.php',
type: 'POST',
data: {
id: getUrlParameter('id'),
formData: JSON.parse('{"' + decodeURI($('#my_form').serialize()).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
},
dataType: 'json',
}).done(function(response) {
});
});
}
Other solution :
function saveExerciseAjaxCall() {
$("#my_form").on("submit", function(event) {
event.preventDefault();
$.ajax({
url: 'saveJson.php',
type: 'POST',
data: $('#my_form').serialize() + "&id=" + getUrlParameter('id'),
}).done(function(response) {
});
});
}
#1
0
You can convert your urlParameter format to Object like this :
您可以将您的urlParameter格式转换为如下对象:
var obj = {
"id": "new",
"formData": "language=nl_NL&getExerciseTitle=3213&question_takeAudio_exerciseWord%5B0%5D=3213&Syllablescounter%5B0%5D=3213S&Syllablescounter%5B1%5D=321"
};
obj.formData = JSON.parse('{"' + decodeURI(obj.formData).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
console.log(obj.formData);
In your code :
在你的代码:
function saveExerciseAjaxCall() {
$("#my_form").on("submit", function(event) {
event.preventDefault();
$.ajax({
url: 'saveJson.php',
type: 'POST',
data: {
id: getUrlParameter('id'),
formData: JSON.parse('{"' + decodeURI($('#my_form').serialize()).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
},
dataType: 'json',
}).done(function(response) {
});
});
}
Other solution :
function saveExerciseAjaxCall() {
$("#my_form").on("submit", function(event) {
event.preventDefault();
$.ajax({
url: 'saveJson.php',
type: 'POST',
data: $('#my_form').serialize() + "&id=" + getUrlParameter('id'),
}).done(function(response) {
});
});
}