【问题标题】:Troubleshooting HTTP request and JSON message on Django对 Django 上的 HTTP 请求和 JSON 消息进行故障排除
【发布时间】:2018-09-13 17:25:53
【问题描述】:

好的,所以我已经做了一段时间了,但我似乎没有取得任何进展。我正在使用 Nginx 和 uwsgi 运行 Django 应用程序。我有一个 http.post,我什至试图阅读我不断出错的项目。

这就是我的 JS 代码的样子:

$scope.receipt_pay_update = function(items)
{ 

  response = confirm("Do you want to continue with the changes?");
if(!response){

  return;
  }
  var data = {
        'items': items,
        'time_now': moment().format("YYYY-MM-DD")
    };
  items.showmessage = true;
  console.log(data)
       $http.post("/foodhub/dashboard/receipt_pay_modal_update", data,{
           data: JSON
       }).
       success(function(data,status,headers,config){
        $scope.alertclass = 'alert-success';
        $scope.save_message_farmer = "Succcessfully update payment"
        console.log("SAVED!")
       }).
       error(function(data,status,headers,config){
        $scope.alertclass = 'alert-danger';
        $scope.save_message_farmer= "Failed to update inventory, please try again"
       })
}

这就是我的 views.py 的样子:

@login_required

def receipt_pay_modal_update(request):
import sys
reload(sys)
sys.setdefaultencoding('utf8')


data = json.loads(request.body)['items']

print data

rec = ReceiverActions.objets.get(identifier = data[0]['identifier'])

rec['paid_check'] = data[0]['paid_status']
rec['date_paid'] = data[0]['paid_date']

rec.save()
return HttpResponse(status=status.HTTP_200_OK)
  1. 我收到无法解码 JSON 的错误。所以我尝试了data = request.body[0],但也没有用。
  2. 有没有其他方法可以在我的服务器上测试小的更改,而无需执行 Git 推送、Git Pull、Python -m compileall 等操作?我问的原因是因为我是通过实践教我这样做的,我觉得有更好的方法。
  3. 在哪里可以查看我的print data

任何帮助将不胜感激。

【问题讨论】:

  • 我收到File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
  • 试试print request.body看看body是否为空
  • 好像是空的。除非我做错了什么。在我发出请求之前的console.log(data) 有数据。
  • 你的 JS 代码有问题

标签: javascript python django http ubuntu


【解决方案1】:

原来我得到的 data 不适合 JSON。我返回并更改了以 json 格式发送数据的请求,并且效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    相关资源
    最近更新 更多