gpt4 book ai didi

javascript - 无法获得 ajax 响应以使用 python 代码放置好

转载 作者:行者123 更新时间:2023-12-03 06:48:25 26 4
gpt4 key购买 nike

问题:出于某种原因,我似乎无法弄清楚为什么如果我的 wo_data 查询为空,我的 ajax 调用不会返回成功值。我想要发生的情况是,如果我将一个值放入 id_work_order 中,并且在我的数据库中找不到它,则会弹出一条警报,指出未找到工作订单 - 否则,如果找到,则返回成功,该成功有效。任何帮助将不胜感激!

如果在我的浏览器控制台中找不到工作订单,我会收到此消息类型错误:数据[0]未定义

如果找到了我会得到这个成功

如果在我的回复中找到工作订单,我的数据如下所示0:对象

purch_order: "1" 
success: "success"
part_rev: "A"
part_number: "12345"
work_o: "W0000001"

客户名称:“测试”

如果没有找到,我的回复中什么也得不到

这是我的观点.py

    def get_work(request): 
if request.is_ajax():
q = request.GET.get('workorder_id', '')
wo_data = Dim_work_order.objects.filter(base_id__icontains = q )[:1]
results = []
for x in wo_data:
x_json = {}
if wo_data.exists():
x_json['success'] = 'success'
x_json['work_o'] = x.base_id
x_json['customer_name'] = x.name
x_json['part_number'] = x.part_id
x_json['part_rev'] = x.part_rev
x_json['purch_order'] = x.customer_po_ref
results.append(x_json)
else:
x_json['success'] = 'workorder_not_found'
results.append(x_json)

data = json.dumps(results)
mimetype = 'application/json'
return HttpResponse(data, mimetype)

else:
data = 'fail'
return render(request, 'app/sheet_form_create')

这是我的workorder_ajax.js

        $(document).ready(function () { 

//$('#id_work_order').click(function () {
// getwork();
//});

$('#work_search').click(function () {
pop_other_fields();
});



//function getwork(){
// $('#id_work_order').autocomplete({
// source: "/sheet/sheet_form_create.html/get_work",
// minLenght: 2,
// });
//}

function pop_other_fields() {
var url = "/sheet/sheet_form_create.html/get_work?workorder_id=" + $('#id_work_order').val();
var work_order = document.getElementById('id_work_order').value;
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
data: '',
success: function (data) {

if (data[0].success = "success") {
console.log(data[0].success);
$('#id_customer_name').val(data[0].customer_name);
$('#id_part_number').val(data[0].part_number);
$('#id_part_revision').val(data[0].part_rev);
$('#id_purchase_order').val(data[0].purch_order);
}
if (data.success = "workorder_not_found") {
alert("workorder not found :(")
}
}

});

}
});

最佳答案

这里的代码永远不会到达:

            else:   
x_json['success'] = 'workorder_not_found'
results.append(x_json)

因为如果 if wo_data.exists(): 不为 true,那么 for x in wo_data: 一开始就不会进行任何迭代。

尝试:

def get_work(request): 
if request.is_ajax():
q = request.GET.get('workorder_id', '')
wo_data = Dim_work_order.objects.filter(base_id__icontains = q )[:1]
results = []
if wo_data.exists():
for x in wo_data:
x_json = {}
x_json['success'] = 'success'
x_json['work_o'] = x.base_id
x_json['customer_name'] = x.name
x_json['part_number'] = x.part_id
x_json['part_rev'] = x.part_rev
x_json['purch_order'] = x.customer_po_ref
results.append(x_json)
else:
results.append({'success': 'workorder_not_found'})

data = json.dumps(results)
mimetype = 'application/json'
return HttpResponse(data, mimetype)

关于javascript - 无法获得 ajax 响应以使用 python 代码放置好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37619002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com