2017-01-27 8 views
0

未知の構文エラーが発生しました:ajaxを実行しているときに予期しない文字列が返され、保存されたデータを含むhtmlテーブルの一番下に行を追加しようとしていますjson。私は本当にそれを見つけることができません....未知の構文エラー: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オブジェクトはコンソールに返されます。表示できません。

(インデックス):845キャッチされないでSyntaxError:予期しない文字列

をし、それは私が上記強調してきたラインを指し、次のように

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) 

私はコンソールに取得していますエラーがちょうどです

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>'; 

私はこれにアプローチする方法が他にわからないよ...

+0

エラーテキストを入力してください。 – Dmitry

+0

私は編集しました。これはChromeデベロッパーコンソールで唯一表示されるエラーです –

+0

jsonキーはほとんど読めません。さらに、すべてのフィールドは 'person'で始まります。これを削除することができます。 – iFlo

答えて

0

あなたがミスしていますjson.personsurname'</td><td>'の間に+を入力します。

一般的に、そのようなHTMLを構築することは本当に悪い考えです。この種のものは見逃しやすいです。

+0

ありがとう、それは私がそれを逃したと信じることができない固定されています。 別の方法がありますか他にどのようにこれを行うか考えることはできません。 –

+0

あなたはforループで構築しようとするかもしれません。しかし、複数の行に分割しても、それぞれが 'html + = ...'を行う場合、より明確になります。 –

+0

お世話になりました! –

関連する問題