My problem is that I have to POST a JSON object in the following format:
{ "key1": "value1",
"key2": 2,
"options": {
"content": "lorem ipsum"
}
}
But I keep getting an Internal server error: Key 'options' not found. This is my js code:
$("#button").click(function(){
var json_test = {
"key1": "value1",
"key2": 2,
"options": {
"content": "lorem ipsum"
}
}
$.ajax
({
type: "POST",
url: '/test/url/',
dataType: 'json',
data:json_test
})
});
And also this is the POST contents from Firebug:
element_type Text
learning_page 1
options[content] lorem ipsum
I'm obviously doing something wrong but I just can't figure out what it is. Any ideas?
I'm not sure but you may be missing stringifying the json
$("#button").click(function(){
var json_test = {
"key1": "value1",
"key2": 2,
"options": {
"content": "lorem ipsum"
}
}
$.ajax
({
type: "POST",
url: '/courses/api/elements/',
dataType: 'json',
data:JSON.stringify(json_test)
})
});
Also try adding
contentType: "application/json; charset=utf-8"
to your jquery options