我正在尝试使用 Tastypie 将数据发送到我的 Django 驱动的服务器。
我有这个模型
class Open(models.Model):
name=models.TextField()
和这个 URLconf
open_resource=OpenResource()
urlpatterns = patterns('',
url(r'^api/', include(open_resource.urls)),
url(r'^admin/', include(admin.site.urls)),
)
当我运行 tastypie curl 命令时
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name","awdawd"}' http://localhost:8000/api/open/
我得到了错误
HTTP/1.0 400 BAD REQUEST
Date: Sat, 05 Apr 2014 12:18:48 GMT
Server: WSGIServer/0.1 Python/2.7.3
X-Frame-Options: SAMEORIGIN
Content-Type: application/json
{"error": ""}
我已经尝试了所有方法,但似乎无法正常工作。
有人知道这个吗?
提前致谢
最佳答案
每当我提供无效的 JSON 数据时,我都会收到这个无用的错误。
正确 JSON 格式为:
{"foo": "bar"} // correct!
[{"foo": "bar"}, {"fiz": "baz"}] // correct!
{"foo": "bar", "fiz": "baz"} // correct!
常见错误示例:
{'foo': 'bar'} // error is using single quotes instead of double quotes
{foo: "bar"} // error is not using double quotes for "foo"
{"foo", "bar"} // error is using a comma (,) instead of a colon (:) ← Your error
更复杂的错误示例:
[{"foo": "bar"}, {"fiz": "baz"},]
// error is using a comma at the end of a list or array
{"foo": "bar", "fiz": "baz",} // courtesy @gthmb
// error is using a comma at the end of the final key-value pair
认为您的 JSON 有效?用 JSON validator 仔细检查.
关于Python (tastypie) - POST 请求错误,返回 "error"dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22880870/