未捕获的语法错误:带有json响应的意外字符串

时间:2017-01-27 12:23:47

标签: python json ajax django

我收到了未被捕获的语法错误:在运行ajax时意外串联了chrome控制台,我试图在html表的底部添加一行,并使用json返回保存的数据。我真的无法发现它......

function create_person() {
    console.log("create person is working!")
    $.ajax({
        url : "{% url 'tande:create_person' %}",
        type: "POST",
        data: { first_name : $('#person-first-name').val(), surname : $('#person-surname').val(), email : $('#person-email').val(), coach_id : $('#person-coach-id').val(), is_coach : $('#person-is-coach').val(), position : $('#person-position').val(), contract_type : $('#person-contract').val()},

        success : function(json) {
            $('#person-first-name').val('');
            $('#person-surname').val('');
            $('#person-email').val('');
            $('#person-coach-id').val('');
            $('#person-is-coach').val('');
            $('#person-position').val('');
            $('#person-contract').val('');
            console.log(json);
            // ERROR OCCURS ON FOLLOWING LINE
            var html = '<tr><td>'+json.personid+'</td><td>'+json.personfirstname+'&nbsp;'+json.personsurname'</td><td>'+json.personposition+'</td><td>'+json.personcontract+'</td><td>'+json.personemail+'</td><td>'+json.personcoachid+'</td></tr>';
            console.log("success"); 
            $('div#talk').html(html);
            console.log(html)
        },

        error : function(xhr,errmsg,err) {
            // $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
            //     " <a href='#' class='close'>&times;</a></div>"); // add the error to the dom
            console.log("uh oh"); 
            }
        });
    };

数据保存成功,json对象返回控制台,我无法显示。

def create_person(request):
    if request.method == "POST":
        print "request post data in view"
        firstname = request.POST.get('first_name')
        print firstname
        lastname = request.POST.get('surname')
        emailadd = request.POST.get('email')
        coachid = request.POST.get('coach_id')
        isacoach = request.POST.get('is_coach')
        positionheld = request.POST.get('position')
        contracttype = request.POST.get('contract_type')

        response_data = {}

        starfruit = Person(first_name=firstname, surname=lastname, email=emailadd, coach_id=coachid, assign_as_coach=isacoach, position=positionheld, contract_type=contracttype)
        starfruit.save()

        response_data['personfirstname'] = starfruit.first_name
        response_data['personsurname'] = starfruit.surname
        response_data['personemail'] = starfruit.email
        response_data['personcoachid'] = starfruit.coach_id
        response_data['personiscoach'] = starfruit.assign_as_coach
        response_data['personposition'] = starfruit.position
        response_data['personcontract'] = starfruit.contract_type
        response_data['personid'] = starfruit.id
        # response_data = {
        # ''
        # }
        print response_data

        return JsonResponse(response_data)

    else:
        print "no post request in view"
        return JsonResponse(response_data)

我在控制台中遇到的错误如下:

(index):845 Uncaught SyntaxError:意外的字符串

它指的是上面突出显示的行

var html = '<tr><td>'+json.personid+'</td><td>'+json.personfirstname+'&nbsp;'+json.personsurname'</td><td>'+json.personposition+'</td><td>'+json.personcontract+'</td><td>'+json.personemail+'</td><td>'+json.personcoachid+'</td></tr>';

我不确定如何处理这个......

1 个答案:

答案 0 :(得分:0)

您在+和以下json.personsurname之间错过'</td><td>'

通常,构建这样的HTML是一个非常糟糕的主意;这种事很容易被遗漏。