首页 > 解决方案 > 对 Django 上的 HTTP 请求和 JSON 消息进行故障排除

问题描述

好的,所以我已经做了一段时间了,但似乎我没有取得任何进展。我正在使用 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

任何帮助将不胜感激。

标签: javascriptpythondjangohttpubuntu

解决方案


原来data我得到的不是 JSON 合适的。我回去更改了将数据作为 json 发送的请求,并且效果很好。


推荐阅读