# Admin Entwicklung: Controller bei Klick auf Button aufrufen (AJAX Call)

**URL:** <https://forum.shopware.com/t/admin-entwicklung-controller-bei-klick-auf-button-aufrufen-ajax-call/62936>\
**Category:** Programmierung\
**Created:** [12. November 2019 um 14:00 UTC](https://forum.shopware.com/t/admin-entwicklung-controller-bei-klick-auf-button-aufrufen-ajax-call/62936 "2019-11-12T14:00:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DenKon26](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@DenKon26](https://forum.shopware.com/u/DenKon26)\
**Post date:** [12. November 2019 um 14:00 UTC](https://forum.shopware.com/t/admin-entwicklung-controller-bei-klick-auf-button-aufrufen-ajax-call/62936/1 "2019-11-12T14:00:28Z")

</div>

Hallo,

&nbsp;

Wie bekomme ich es hin, bei einem Klick auf einen Button in einem eigenen Admin Module einen bestimmten Controller bzw. eine entsprechende Route aufzurufen?

Konkret wünsche ich mir folgendes:

```
    Verbindung testen

```

Und auf der entsprechenden Page im eigenen Module:

&nbsp;

```
Component.register('testmodule-detail'), {

    // ...

    methods: {

        onClickTestConnection() {
            // HIER ein Ajax Call auf einen bestimmten Controller
        }
    }
});

```

&nbsp;

jQuery ist nicht möglich oder? Welche Möglichkeiten habe ich?

&nbsp;

Vielen Dank und viele Grüße  
Dennis

---

<div class="post-metadata">

**Author:** ![oerkel](https://avatars.discourse-cdn.com/v4/letter/o/a9adbd/32.png) [@oerkel](https://forum.shopware.com/u/oerkel)\
**Post date:** [13. November 2019 um 08:30 UTC](https://forum.shopware.com/t/admin-entwicklung-controller-bei-klick-auf-button-aufrufen-ajax-call/62936/2 "2019-11-13T08:30:39Z")

</div>

Hi,  
ich habe gerade das gleiche Thema. Mit fetch kann man den Zugriff starten, allerdings fehlt mir noch die übergaben der Authentification Header.

&nbsp;

&nbsp;

```
            fetch('/api/v1/_action/controller/my-api-action', { headers: { "Content-Type": "application/json; charset=utf-8" }})
            .then(res => res.json()) // parse response as JSON (can be res.text() for plain response)
            .then(response => {
                // here you do what you want with response
            })
            .catch(err => {
                console.log("u")
                alert("sorry, there are no results for your search")
            });

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![Stephan\_Pohl](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/stephan_pohl/32/10212_2.png) [@Stephan\_Pohl](https://forum.shopware.com/u/Stephan_Pohl)\
**Post date:** [13. November 2019 um 09:19 UTC](https://forum.shopware.com/t/admin-entwicklung-controller-bei-klick-auf-button-aufrufen-ajax-call/62936/3 "2019-11-13T09:19:27Z")

</div>

Morgen zusammen,

ihr könnt euch unseren vorkonfigurierten HTTP Client über unseren „init“-Container rausziehen:

```
const httpClient = Shopware.Application.getContainer('init').httpClient;

```

Viele Grüße,  
Stephan&nbsp; ![Shopware](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/shopware.png "Shopware")

---

<div class="post-metadata">

**Author:** ![DenKon26](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@DenKon26](https://forum.shopware.com/u/DenKon26)\
**Post date:** [13. November 2019 um 09:53 UTC](https://forum.shopware.com/t/admin-entwicklung-controller-bei-klick-auf-button-aufrufen-ajax-call/62936/4 "2019-11-13T09:53:59Z")

</div>

> [@Stephan Pohl schrieb:](https://forum.shopware.com/profile/2/Stephan%20Pohl "Stephan%20Pohl")
> 
> Morgen zusammen,
> 
> ihr könnt euch unseren vorkonfigurierten HTTP Client über unseren „init“-Container rausziehen:
> 
> const httpClient = Shopware.Application.getContainer(‚init‘).httpClient;
> 
> Viele Grüße,  
> Stephan&nbsp; ![Shopware](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/86c57fbace799dd4eb1f1b8a2d3bdc1733f0aea1.png "Shopware")

Gibt es dazu eine Doku zur Verwendung?

&nbsp;

Wie würde ich denn als praktisches Code-Beispiel einen Call auf meinen eigenen Controller hinbekommen?

index.js:

```
onClickTestConnection() {
    var httpClient = Shopware.Application.getContainer('init').httpClient;

    
    var connection = {
        'dbName': this.testConfiguration.dbName,
        'dbHost': this.testConfiguration.dbHost,
        'dbUser': this.testConfiguration.dbUser,
        'dbPassword': this.testConfiguration.dbPassword,
        'dbPort': this.testConfiguration.dbPort
    };

    // ... was nun? Wie kriege ich den POST Call auf meinen Controller hin? Wo muss ich die connection Parameter anfügen?

    httpClient.post('/_action/admin/testcontroller/testconnection'); // was noch?
}

```

&nbsp;

TestController.php:

```
// ...

class TestController extends AbstractController
{

    /**
     *
     * @RouteScope(scopes={"administration"})
     * @Route("/api/v{version}/_action/admin/testcontroller/testconnection", name="admin.testcontroller.testConnection", options={"seo"="false"}, methods={"POST", "GET"})
     */
    public function testConnection(Request $request)
    {

        // wie rufe ich die Parameter ab?

    }
}

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![DenKon26](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@DenKon26](https://forum.shopware.com/u/DenKon26)\
**Post date:** [13. November 2019 um 09:59 UTC](https://forum.shopware.com/t/admin-entwicklung-controller-bei-klick-auf-button-aufrufen-ajax-call/62936/5 "2019-11-13T09:59:56Z")

</div>

EDIT:

&nbsp;

ich habe nun versucht meine connection als zweiten Parameter an den Post zu hängen:

```
var httpClient = Shopware.Application.getContainer('init').httpClient;

var connection = {
    'dbName': this.testConfiguration.dbName,
    'dbHost': this.testConfiguration.dbHost,
    'dbUser': this.testConfiguration.dbUser,
    'dbPassword': this.testConfiguration.dbPassword,
    'dbPort': this.testConfiguration.dbPort
};

httpClient.post("/_action/admin/testcontroller/testconnection", connection);

```

&nbsp;

In der Netzwerkkonsole wirft der Aufruf einen 401: " The resource owner or authorization server denied the request. Detail: Missing „Authorization“ header "

&nbsp;

Anschließend erfolgt ein Aufruf auf „Token“ der mir den API Token zurückliefert und danach gibt es erneut eine Request auf meine testconnection die dann mit Code 200 erfolgreich ist.

&nbsp;

Wie kriege ich es also hin, dass mir direkt beim ersten Request auf testconnection ein Token erzeugt wird, so dass ich nur einen Call habe und keine 401 Fehler?

---

<div class="post-metadata">

**Author:** ![oerkel](https://avatars.discourse-cdn.com/v4/letter/o/a9adbd/32.png) [@oerkel](https://forum.shopware.com/u/oerkel)\
**Post date:** [13. November 2019 um 12:24 UTC](https://forum.shopware.com/t/admin-entwicklung-controller-bei-klick-auf-button-aufrufen-ajax-call/62936/6 "2019-11-13T12:24:48Z")

</div>

ich habe das ganze mit dem Beispiel aus dem Tutorial gelöst.  
[Shopware 6: Basic handling of js services in the administration](https://docs.shopware.com/en/shopware-platform-dev-en/how-to/decorate-administration-js-service?category=shopware-platform-dev-en/how-to)  
&nbsp;

Aber auch da den Effekt mit 401 Header
