Huz*_*afa 7 javascript ajax symfony symfony-routing shopware
StorefrontController我正在尝试向我的客户发送有关最新 Shopware 的请求6.2.2,但收到以下错误:
PageController can't be requested via XmlHttpRequest.
Run Code Online (Sandbox Code Playgroud)
我正在httpClient从自定义 JS 插件发出常规请求:
export default class MyCustomPlugin extends Plugin {
static options = {
dataUrl: '', // comes from the twig as "path('frontend.path.to.route')"
product: null,
params: {},
loadingIndicatorClass: 'is-loading',
responseSelector: 'some-selector-class'
}
init () {
// this.el.innerHTML = LoadingIndicator.getTemplate()
this.httpClient = new HttpClient()
const query = querystring.stringify(this.options.product)
this.sendDataRequest(query)
}
/**
* Add classes to add loading styling.
* Prevents the user from clicking filter labels during filter request.
*/
addLoadingIndicatorClass () {
this.el.classList.add(this.options.loadingIndicatorClass)
}
/**
* Remove loading styling classes.
*/
removeLoadingIndicatorClass () {
this.el.classList.remove(this.options.loadingIndicatorClass)
}
/**
* Send request to get filtered product data.
*
* @param {String} filterParams - active filters as querystring
*/
sendDataRequest (filterParams) {
this.addLoadingIndicatorClass()
this.httpClient.abort()
this.httpClient.get(`${this.options.dataUrl}?${filterParams}`, (response) => {
this.renderResponse(response)
this.removeLoadingIndicatorClass()
})
}
/**
* Inject the HTML of the response to the element.
*
* @param {String} response - HTML response
*/
renderResponse (response) {
ElementReplaceHelper.replaceFromMarkup(response, this.options.responseSelector, false)
window.PluginManager.initializePlugins()
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的StorefrontController路线:
/**
* @Route("/path/to_route", name="frontend.path.to.route", methods={"GET"})
*/
public function someAction(Request $request, Context $context): JsonResponse
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么请求没有通过?我想向 Shopware 6 中我自己的控制器发送一个简单的 AJAX 请求。
谢谢!
小智 6
尝试添加:
defaults={"XmlHttpRequest"=true}到您的路线。
因此,更改后您将拥有:
/**
* @Route("/path/to_route", name="frontend.path.to.route", methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/
Run Code Online (Sandbox Code Playgroud)
还要确保使用@RouteScope注释在控制器中正确定义范围。由于您的代码是店面 JavaScript 插件,因此它应该storefront定义范围。
| 归档时间: |
|
| 查看次数: |
2858 次 |
| 最近记录: |