191114Ajax.md

A, Ajax strings, numbers, Boolean type interact with the backend

  • ajax submit
$.ajax({
    url: '/del_student/',
    type: 'GET',
    data: {'nid': 2},  //此处数据类型是int
    success: function (arg) {
        var dict = JSON.parse(arg);
        if(dict.status){
            $('tr[nid="'+ rowId +'"]').remove();
        }
        $('#delModal').modal('hide');
    }
})
  • receive
def del_student(request):
    ret = {'status': True}
    try:
        nid = request.GET.get('nid')
        models.Students.objects.filter(id=nid).delete()
    except Exception as e:
        ret['status'] = False
    return HttpResponse(json.dumps(ret))

Two, Ajax list of data types to interact with the backend

  • ajax submit
$.ajax({
    url: '/del_student/',
    type: 'GET',
    data: {'nid': [2,3,4,5]},  //此处数据类型是list
    traditional: true  //传数组需要加此项
    success: function (arg) {
        var dict = JSON.parse(arg);
        if(dict.status){
            $('tr[nid="'+ rowId +'"]').remove();
        }
        $('#delModal').modal('hide');
    }
})
  • receive
def del_student(request):
    ret = {'status': True}
    try:
        nid = request.GET.getlist('nid')  #需要使用getlist接收
        models.Students.objects.filter(id=nid).delete()
    except Exception as e:
        ret['status'] = False
    return HttpResponse(json.dumps(ret))

Ajax POST submit only supports strings, numbers, arrays, dictionaries do not support the type of data

It can be converted to a string if you must use a dictionary:

data: {'k1': JSON.srtingify({'k2':'v2'})}

Guess you like

Origin www.cnblogs.com/L-dongf/p/12146223.html