总结一下最近解决的问题:也就是前端的传参问题,如果不好解决,我的建议就是转换成全局变量。
这里我想要将task_json这个数据,post到后台的assign函数,这里assign函数的url是(如下):
url(r'^assign', view.assign)
首先,你需要了解ajax和post,简单来说ajax一共含有的两个data,这里我们需要了解:第一个data是前端想要传到后端的数据,比如:
data: $jsTagsform.serialize(),第二个data,是你传到后台,成功返回的数据:
success: function (data){......}我直接讲我的解决思路吧,我碰壁的过程就先不提了:总之,就是变量全局化!!
即使我需要的参数在success: function (data){}中,一般这种情况下会是一个局部变量,这里有一个方法。首先,我在 这个(导入响应)function submitall(){}外边定义:
window.task_json = "";ok,这样你定义了一个全局参数,它叫做task_json,目前是一个空的,第二步,到你需要局部参数变成全局参数,进一步你能post你想要的后台函数:
success: function (data) {
var id = data.id;
window.task_id = id;
var task_json=data.task_json;
{#xjy_create: 列表(list)转换为字符串(string)#}
string_task_json = JSON.stringify(task_json);
window.task_json = string_task_json;
{#xjy_create_end#}
......
}data.task_json是从后台一个add_task函数返回来的,我的目的就是:获取它,post到assign函数!这里可以积累一下,前端中list转换为string类型:
string = JSON.stringify( list )window.task_json = string_task_json; 这样我task_json是含参的全局变量了。
=========================================================================
接下来,是我对于我前端后端项目自己的记录了:
前端代码,获取含参的全局参数,并向后台post(你想要的url)
//将含参的全局参数 赋值给task_json
task_json = window.task_json;
if (uploader.files.length > 0) {
for (o = 0; o < uploader.files.length; o++) {
//这里是向后台assign函数 POST
$.post("assign",
{
// id是任务标签(id:一批任务的便签id)
id: id,
// task_json: 我想传的树形结构信息
task_json:task_json,
// 数据类型(rawdata、Image、Video)
dataType: $("#" + uploader.files[o].id + "dataType").find("option:selected").text(),
// 密级(public、inside、secret、topsecret......)
dataSecret: $("#" + uploader.files[o].id + "secret").find("option:selected").val()
}
)
}
}后台assign代码:
@csrf_exempt
def assign(request):
if request.method == "POST":
username = request.session.get('username')
password = request.session.get('password')
sessionid = request.session.get('sessionid')
cookies = dict(sessionid=sessionid)
task_id = request.POST.get("id")
dataType = request.POST.get("dataType")
rawdata = request.POST.get("rawdata")
curve = request.POST.get("curve")
image = request.POST.get("image")
vedio = request.POST.get("vedio")
condition = request.POST.get("condition")
tf = request.POST.get("tf")
dataSecret = request.POST.get("dataSecret")
# xjy-create:这里是获取task_json的代码部分
task_json = request.POST.get("task_json")
print ("=======html Post task_json========")
print task_json
task_json_list = json.loads(task_json)
print (type(task_json_list)) #[{"t_name":"level_1","type_id":"2","level":1} , {"t_name":"level_2","type_id":"4","level":2}]
id = request.POST.get("id")
print ("******" + id)
# 目前只需要get到 type_id(树形结构: 实验类型一、实验类型二)
type_id = []
for i in task_json_list:
type_id.append(i['type_id'])
print type_id # [u'2', u'4']
# 获取一一对应的关系:后续我需要这里与权限系统交互
test_Pattern_dict = {'1': 'LeixingA', '2': 'LeixingB', '3': 'LeixingC'}
test_Pattern = test_Pattern_dict[type_id[0]]
print test_Pattern
# xjy-create_end
headers = {
'Accept': 'application/json'
}
nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
打印出来的结果如下:
<< -------------------------can: assign(request)----------------------------------->>
=======html Post task_json========
[{"t_name":"level_1","type_id":"2","level":1},{"t_name":"level_2","type_id":"4","level":2}]
type of task_json_list
<type 'list'>
******22bd5566365411ec91f80242ac110002
[u'2', u'4']
LeixingB