如何将对象的id发送到后端功能?

时间:2017-09-28 11:40:18

标签: javascript ajax request frontend backend

我对前端的了解并不是那么好,我不知道如何向后端发送id数组,这将获得所有数据并将其打开到新页面。请帮忙

我的功能:

@has_session_params('tin')
def print_documents(request):
    doc_ids = request.POST.getlist('doc_ids[]')
    tin = request.session['tin']
    params = dict()
    template = 'documents/documents_to_print.html'
    params['Docs'] = docs
    params['pb'] = pb
    params['is_pdf'] = request.GET.get('is_pdf', 'false')
    params['host'] = request.scheme + "://" + request.META["HTTP_HOST"]
    params['url'] = settings.SITE_URL + request.get_full_path()
    params['doc_type'] = INVOICE_TYPE
    invoice_list = list()
    for doc_id in doc_ids:
        response = proxy.get_invoice_by_id(invoice_id=doc_id, tin=tin)
        if response.status.code != pb.ResponseStatus.OK:
            response = proxy.get_invoice_draft_by_id(
                invoice_draft_id=doc_id, tin=tin)
        invoice_list.append(response.invoice)
    params['invoices'] = invoice_list
    return render(request, template, params)

我知道如何通过id获取一个对象,它将是:

def print_document(request, doc_id):

之类的东西,以及函数的url看起来像那样:

url(r'^print_documents/(?P<doc_id>[a-z\d]{24})/$', invoices.print_documents, name='print_documents')

和新页面链接将类似 localhost:8000 / documents / print_documents / {doc_id}

这个方法我可以这样称呼:

<a href="{url documents:print_documents doc_id}">print</a>

但现在我想选择几个文档并发送doc_ids,我知道我可以喜欢上面的方法,但我不想要大链接。以及如何正确地将ID数组发送到后端?

现在我有类似的东西:

printAll.on("click", function(){
var invoicesID = [];
$('input.select-inv-check:checked').each(function() {
    invoicesID.push($(this).data('docid'));
});

var url = Globals.printAll;

$.post( url, { doc_ids: invoicesID } ).done(function(result) {
    console.log("result", result[0].Docs);
     if(result.code == 0){

     }else{
         alert(result.message);  
     }
});
});

我知道它不正确!请帮忙

0 个答案:

没有答案