Typo3 9.x Ajax调用

Configuration about a single route for ajax call: getamministrazioni.json

I tried to change configuration site as follow:

...


    routeEnhancers:
      News:
        type: Extbase
        extension: News
        plugin: Pi1
        routes:
          -
            routePath: '/{news-title}'
            _controller: 'News::detail'
            _arguments:
              news-title: news
        aspects:
          news-title:
            type: PersistedAliasMapper
            tableName: tx_news_domain_model_news
            routeFieldName: path_segment
      PageTypeSuffix:
        type: PageType
        default: .html
        map:
          .html: 0
          getamministrazioni.json: 1035343

    errorHandling: {  }
    routes: {  }
...

And in setup.typoscript i have:

GetAmministrazioni = PAGE
GetAmministrazioni {
typeNum = 1035343
config {
    disableAllHeaderCode = 1
    debug = 0
    no_cache = 1
    additionalHeaders {
      10 {
        header = Content-Type: application/json
        replace = 1
      }
    }
}
 10 < tt_content.list.20.my_controller_getamministrazioni

}

It works but for all pages.

  1. /home/getamministrazioni.json
  2. /page1/getamministrazioni.json

etc.. etc..

I want a single route from root '/getamministrazioni.json

how i can do that?

There is a possibility to limit the routing to specific page ids:

limitToPages: 1

But this will limit your whole mapping configuration to page id 1, also the .html suffix (which you don't want to, I guess).

Unfortunately, it is currently not possible to create multiple Route Enhancers with the same name, like in the following, non-working example:

PageTypeSuffix:
  type: PageType
  map:
    .html: 0
PageTypeSuffix:
  type: PageType
  limitToPages: 1
  map:
    sitemap.xml: 1533906435

Two possible workarounds:

  1. Create your own RouteEnhancer, which just extends TYPO3\CMS\Core\Routing\Enhancer\PageTypeDecorator to allow a different name (see custom enhancers)

  2. Redirect your json page type to an error page, if page id is not 0 (no routing neccessary, as TypoScript avoids delivering of this page type)

    [getTSFE().id != 1] seo_sitemap.config { additionalHeaders.10 { header = Location: /error.html } } [END]

I found another solution. I create a plugin and i use controller to print json and i 'return' false. In template setup strip all html and i change header content type. So in every page i insert a a plugin as content that print the json