2016-10-05 4 views
0

먼저 다른 제안에 따라 필자는 preventDefault()를 둘러 보았지만 성공하지 못했습니다.백그라운드에서 작업하는 대신 Ajax 폼을 다시로드하는 중

내 의견 양식은 양식 필드에 "process.php"를 전달하고 관련 메시지를 반환하기 위해 자바 스크립트를 사용합니다 (예 : "sucesss"또는 "실패")

그것은 것을 제외하고 그 일을 대신에 머물고 이와 그 부하 'process.php'페이지 'feedback.php'같은 페이지에 ... { "데이터": "데이터 값"}

을 Heres 코드 :

$(document).ready(
function(){ 
    $('form').submit(
     function(event){ 
      var formData = 
      { 
       'name' : $('input[name=name]').val(), 
       'page' : $('input[name=page]').val() 
      }; 

      $('.form-group').removeClass('has-error'); // remove the error class 
      $('.error').remove(); // remove the error text 

      event.preventDefault(); 

      $.ajax({ 
        type  : 'POST', // define the type of HTTP verb we want to use (POST for our form) 
        url  : 'process.php', // the url where we want to POST 
        data  : formData, // our data object 
        dataType : 'json', // what type of data do we expect back from the server 
        encode : true 
       }) 

      .done(function(data){ 
        if (! data.success) 
        { 
         if (data.errors.name) 
         { 
          $('#name-group').addClass('has-error'); // add the error class to show red input 
          $('#nameField').append(data.errors.name); // add the actual error name under our input 
         } 
        } 
        else 
        { 
         $('form').append('<div class="buttonError">Message Sent!</div>'); 
         form.myButton.disabled = true; 
        } 
       } 
      ); 

      .fail(function(data){ 
        $('form').append('<div class="buttonError">Failed!</div>'); 
       } 
       console.log(data); 
      ); 
      event.preventDefault(); 
     } 
    ); 
}); 
+0

이 오류에 대한 귀하의 브라우저의 콘솔을 확인하십시오. 페이지 내비게이션 후에 * "Preserve log"* (또는 귀하의 브라우저와 동등한)를 사용하여 메시지를 보관해야 할 수도 있습니다. – Phil

+0

양식을 게시하십시오. – snit80

+1

'.fail' 주변에 구문 오류가있는 것 같습니다. 아래'**'** console.log'를 닫으십시오. ** 오타로 닫는 투표 ** – Phil

답변

-1

는 방법을 제거 및 HTML 파일의 form 태그에있는 조치. 이것은 괜찮아 보인다. 작동하지 않는지 알려주세요.

+0

이것은 문제를 해결하지 못합니다. 실제로, 그것은 현재 페이지에 만들어진'GET' 요청으로 양식을 변환하기 때문에 작동하지 않을 것입니다. OP는 자신의 JS 오류를 수정해야합니다 – Phil

+0

@ Phil 그들은이 경우에 콘솔을 사용해야합니다. – borngeek

+0

[농담 없음] (http://stackoverflow.com/questions/39864824/ajax-form-reloading-page-instead-of-working-in-the-background/39864868?noredirect=1#comment67017520_39864824) – Phil

0

은 $ 아약스를 호출하는 구문 오류가, 그것은 다음과 같이 수정해야 다음과 같습니다

$.ajax({ 
     type  : 'POST', // define the type of HTTP verb we want to use (POST for our form) 
     url  : 'process.php', // the url where we want to POST 
     data  : formData, // our data object 
     dataType : 'json', // what type of data do we expect back from the server 
     encode : true 
    }).done(function(data){ 
     if (! data.success) 
     { 
      if (data.errors.name) 
      { 
       $('#name-group').addClass('has-error'); // add the error class to show red input 
       $('#nameField').append(data.errors.name); // add the actual error name under our input 
      } 
     } 
     else 
     { 
      $('form').append('<div class="buttonError">Message Sent!</div>'); 
      form.myButton.disabled = true; 
     } 
    }).fail(function(data){ 
     $('form').append('<div class="buttonError">Failed!</div>'); 
     console.log(data); 
    });