1 回答

TA貢獻1847條經驗 獲得超11個贊
謝謝我使用 webpack encore 成功了。幫助 :
安裝 Fos jsrouting 包
step 1 : expose your route in your controller otherwise you have to modify the file: fos_js_routes.json in the public folder or rename the route
/**
* @Route("/country", **options={"expose"=true},** name="ajaxCountry")
**/
step 2 :
import Routing from '../../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';
const routes = require('../../../public/js/fos_js_routes.json');
Routing.setRoutingData(routes);
var formData = new FormData();
var urlAjax = Routing.generate('yourRoute');
fetch(urlAjax, {
method: 'POST',
body: formData
})
.then(function (response) {
return response.json();
})
.then(function (message) {
...
step 3 : in your terminal
php bin/console fos:js-routing:dump --format=json --target=public/js/fos_js_routes.json
再見
添加回答
舉報