Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Django中解析通过AJAX发送的JSON对象_Json_Ajax_Django_Django Views - Fatal编程技术网

在Django中解析通过AJAX发送的JSON对象

在Django中解析通过AJAX发送的JSON对象,json,ajax,django,django-views,Json,Ajax,Django,Django Views,这是我创建json文件的代码: $( ".save" ).on("click", function(){ var items=[]; $("tr.data").each(function() { var item = { itemCode : $(this).find('td:nth-child(1) span').html(), itemQuantity : $(this).find('td:nth-child(4) span').html() }; item

这是我创建json文件的代码:

$( ".save" ).on("click", function(){
var items=[];


$("tr.data").each(function() {

 var item = {
  itemCode : $(this).find('td:nth-child(1) span').html(),
  itemQuantity : $(this).find('td:nth-child(4) span').html()        
 };
items.push(item);       
 });
});
现在json对象看起来像:

[{"itemcode":"code1","itemquantity":"quantity1"},{"itemcode":"code2","itemquantity":"quantity2"},...]
我的问题是如何在Django视图中解析这些数据

以下是我的AJAX函数供参考:

(function() {
    $.ajax({
        url : "", 
        type: "POST",
        data:{ bill_details: JSON.stringify(items),
                calltype:'save'},
        dataType: "application/json", // datatype being sent

        success : function(jsondata) {  

            //do something
        },

            error : function() {
                //do something
            }
    });
}());
因为我要向同一个视图发送多个AJAX请求,所以我还需要“calltype”数据

谢谢你的回答!!顺便说一句,我非常需要知道这个简单的东西,我错过了

这是我用于分析的代码段:

if (calltype == 'save'):
        response_data = {}
        bill_data = json.loads(request.POST.get('bill_details'))
        itemcode1=bill_details[0]['itemCode'] 
        #this part is just for checking
        response_data['name'] = itemcode1
        jsondata = json.dumps(response_data)
        return HttpResponse(jsondata)
引发的错误是

string indices must be integers
请求你的帮助

供参考,这是我的帖子回复(摘自回溯):

编辑的Django视图

这是我编辑的视图:

        if (calltype == 'save'):
        bill_detail = request.POST.get('bill_details')
        response_data = {}
        bill_data = json.loads(bill_detail)
        itemcode1=bill_data[0]['itemCode'] 
        #this part is just for checking
        response_data['name'] = itemcode1
        jsondata = json.dumps(response_data)
        return HttpResponse(jsondata)
我不理解这个问题。为了解决这个问题,我的问题是:get调用返回的数据类型是什么,json.loads的输入数据类型应该是什么。Bcoz显示的错误是json。加载文件必须是字符串类型!!(深陷困境) 错误:


其他呼叫类型有哪些?您应该分离视图,创建视图只创建,只更新,等等。使用CBV和视图继承,这可以以非常枯燥的方式实现。对于每种调用类型都有不同的url,您不需要在json请求中发送calltype参数(同时避免硬编码值,如在js级别和视图级别上声明的“save”),只需更改发布数据的url即可。可能会重复Hey jabez,谢谢。我正在学习CBV——也许任何关于最佳资源的提示都会有所帮助。但到目前为止,我能找到一个解决问题的办法吗?伙计们,这不是重复的,因为在尼特长大的爱欲者得到了其他答案的解决。顺便说一句,我已经使用了python的json.loads()函数。我已经运行了你的代码,在我更正了拼写bill\u details/bill\u data、itemCode/itemCode等之后,对我来说效果很好。我在你的回溯中看到,bill_的详细信息不是一个列表,而是一个字符串。
        if (calltype == 'save'):
        bill_detail = request.POST.get('bill_details')
        response_data = {}
        bill_data = json.loads(bill_detail)
        itemcode1=bill_data[0]['itemCode'] 
        #this part is just for checking
        response_data['name'] = itemcode1
        jsondata = json.dumps(response_data)
        return HttpResponse(jsondata)
the JSON object must be str, not 'NoneType'