2016-01-05 15 views
0

我有一個接收JSON請求的控制器方法。控制器返回提交表單的數據,然後將更新後的數據異步加載到頁面中。然後,表單可以重新提交任意次數而無需重新加載頁面。帶AJAX的Symfony 2:向控制器提交格式化請求方法

我遇到的問題與控制器方法正在讀取的JSON數據在初始表單提交後有所不同。

相關的控制器代碼:

/** 
* Creates a new HoursSpecial entity. 
* 
* @Route("/", name="hoursspecial_postcreate") 
* @Method("POST") 
*/ 
public function postCreateAction(Request $request){ 
    $requestData = $request->request->all(); 

    $date = $requestData['eventDate']; 

    ... 
} 

這裏是AJAX代碼:

/* Handle submission of special hours form */ 
$('.specialHours_form').on('submit', function(event){ 
    var targetForm = $(this); 

    event.preventDefault(); 

    ajaxObject = { 
     url: $(this).attr("action"), 
     type: 'POST', 
     contentType: "application/json; charset=UTF-8", 
     data: JSON.stringify({"openTime":$("#appbundle_hoursspecial_openTime", this).val(), "closeTime":$("#appbundle_hoursspecial_closeTime", this).val(), "status":$("input[name^='appbundle_hoursspecial']:checked", this).val(), "event":$("#appbundle_hoursspecial_event").val(), "area":$("#appbundle_hoursspecial_area", this).val(), "eventDate":$("#appbundle_hoursspecial_eventDate", this).val()}) 
    } 

    $.ajax(ajaxObject) 
     .success(function(data,status,xhr) { 
       console.log(status); 
       $('#special_hours_container').html(data); //show special hours for chosen week 
       $('#dynamic_daterange').html(' Week of ' + $('#special_firstday').html() + ' thru ' + $('#special_lastday').html() + ' '); 
       makeTimepicker('.specialHours_form', '#appbundle_hoursspecial_openTime, #appbundle_hoursspecial_closeTime'); //apply timepicker to special hours elements 
       $('span.label-success, span.label-danger').remove(); 
     }) 
     .fail(function(data,status,xhr) { 
       $('span.label-success, span.label-danger').remove(); 
       $('#appbundle_hoursspecial_submit', targetForm).after('<span class="label label-danger"><span class="glyphicon glyphicon-remove"></span></span>'); 
       console.log(status); 
       console.log("dangah!"); 
     }) 
     .always(function(data,status,xhr) { 
       console.log(status); 
     }); 
}); 

所以,如果我身邊提交此表在第一時間和運行$的RequestData的的var_dump,我得到(使用樣本數據):

array(6) { ["openTime"]=> string(5) "01:00" ["closeTime"]=> string(5) "11:00" ["status"]=> string(1) "0" ["event"]=> string(1) "1" ["area"]=> string(2) "10" ["eventDate"]=> string(10) "2016-01-04" } 

但是,如果我去做了第二次,該數據被放置在第二陣列,其關鍵的裏面是我提交表單的名字:我結束了錯誤

array(1) { ["appbundle_hoursspecial"]=> array(7) { ["openTime"]=> string(5) "00:30" ["closeTime"]=> string(5) "02:00" ["status"]=> string(1) "0" ["eventDate"]=> string(10) "2016-01-04" ["area"]=> string(2) "10" ["event"]=> string(1) "1" ["submit"]=> string(0) "" } } 

而且很明顯:

Notice: Undefined index: eventDate 

必須有這是一個原因,我無法弄清楚它可能是什麼!我知道我可以在我的控制器中檢查appbundle_hoursspecial鍵的存在,但是如果我不需要,我寧願不要。

+0

使用'變種ajaxObejct',否則你定義'ajaxObject'爲全局變量。不確定它是否與你的問題有關,但它可能有幫助。 –

+1

爲什麼在json中發佈數據?你可以很容易地使用普通的後期字段,就像你發佈你的表單沒有ajax一樣:$ .post('server.php',$('#theForm')。serialize()) –

+0

@Frankbeen我正在使用FOSRestBundle並擁有它設置爲以JSON格式要求POST數據。 – Ravioli87

回答

0

這可能與您渲染的表單有關。

$('#special_hours_container').html(data); 

我假設數據實際上是表單的HTML。可能在你的表單類型,你有這樣的:

// HoursSpecialFormType or something 

... 

public function getName() 
{ 
    return 'appbundle_hoursspecial'; 
} 

這將導致所有這一切得到插入到頁面作爲新的形式:

<input name="appbundle_hoursspecial[openTime]" type=text /> 

那麼當你的數據被再次提交它會被包裹在該陣列中。

爲了解決這個問題

只要不提供窗體名稱。

public function getName() 
{ 
    return ''; 
}