【问题标题】:AngularJS restmod: POST request to Django REST of string with "?a=b"AngularJS restmod:对带有“?a = b”的字符串的Django REST的POST请求
【发布时间】:2015-01-18 03:42:28
【问题描述】:

我正在使用 angular-restmodPOST 请求发送到 Django REST Framework 后端。但每当包含? 的字符串将导致POST 数据损坏(收到时):例如,假设我正在发送POST 数据

{"properties": "values", "status": "?a=3", "other properties": "other values"}

后端会收到request.data

MergeDict(<QueryDict: {u'{"properties": "values", "status": "?a': [u'3", "other properties": "other values"']}>, <MultiValueDict: {}>)

事情在=开始分崩离析。

PUT 方法在这种情况下可以正常工作。我想知道如何解决这个问题?

========== 编辑 ========== 最后我找到了$http的旧设置:

# $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"

覆盖默认的JSON 内容类型restmod。我删除了它,问题就解决了。

【问题讨论】:

    标签: django angularjs django-rest-framework


    【解决方案1】:

    虽然我不知道 anuglar-restmod 是如何工作的,但看起来您正在尝试发送类似 JSON 的数据,但它实际上是作为表单编码数据发送的。数据进行表单编码时,组合成key1=val1[&amp;key2=val2...],并在请求正文中作为一个长字符串发送。

    因此,当您期望 JSON 数据以以下方式发送时

    {"properties": "values", "status": "?a=3", "other properties": "other values"}
    

    它实际上是作为表单编码数据发送的,采用

    的形式
    {"properties": "values", "status": "?a=3", "other properties": "other values"
    

    这应该使您的问题实际上是显而易见的:它不是 实际上是形式编码的。这是被分解的,{"properties": "values", "status": "?a 是键,3", "other properties": "other values" 是值。

    这是may be the fault of the tools you are using 的一部分,但有一种简单的方法可以解决它:make sure you are using JSON

    【讨论】:

    • 谢谢。事实上,Chrome 显示 POST 数据是以表单数据形式发送的。让我看看如何修改它。
    猜你喜欢
    • 1970-01-01
    • 2016-07-09
    • 2019-11-20
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    相关资源
    最近更新 更多