Not*_*mon 5 python django ajax jquery django-rest-framework
我正在使用Django的REST框架来支持API,并使用jQuery向它发送Ajax请求.
以下cURL工作正常:
curl -X POST -d '{"timeLogMins": 30, "personid": 3, "projectid": 8, "timeLogStart_dtm": "2013-07-18"}' -H "Content-Type: application/json" -u user:password http://localhost:8000/api/timelogests/
Run Code Online (Sandbox Code Playgroud)
但是,当我使用jquery进行ajax调用时,我收到错误
$.ajax({
url: update_url2,
contentType:"application/json",
headers: {
"Authorization": "Basic " + window.btoa("user:password"),
},
dataType: "json",
data: {
"timeLogMins":30,
"personid":personid,
"projectid":projectid,
"timeLogStart_dtm":start_date
},
type: 'POST',
}).error(function(r){ console.log(r) })
.success(function(r){ console.log("success", r) })
Run Code Online (Sandbox Code Playgroud)
设置标题工作正常,但数据似乎返回:
"{"detail": "JSON parse error - No JSON object could be decoded"}"
Run Code Online (Sandbox Code Playgroud)
我昨天开始使用REST Framework,所以还是很新的.有没有办法拦截请求标头并检查JSON?REST Framework请求对象不是很明显.
事实证明,将发送的数据转换为实际字符串将使框架可以读取JSON.根据jQuery文档,这应该以任何一种方式发生,但它似乎打破了这个特定的数据对象的布局?这解决了我的问题,但我不确定是什么问题.