简体   繁体   中英

Yii2: data-method='post' is sending GET request

I am making changes in existing web page in yii2. I had this section of code:

Html::a('Confirm!',[
        'default/apply',
        'confirm' => 1,
        'id' => $data->id
    ],['class' => 'btn-primary','data-method' => 'post'])

I have moved this to a different container on the same page.

(I had to adjust slightly, changing $data->id into $projectInfo->id as earlier it was inside anonymous function within a widget and now inside a foreach loop. But this should not be relevant I suppose.)

Both before and after the change the same line is present in html (but in different part of the page):

<a class="btn-primary" href="/participant/default/apply/13/1" data-method="post">Confirm!</a>

But on execution http request is now sent as GET instead of POST.

BEFORE: "POST /participant/default/apply/13/1 HTTP/1.1"

NOW: "GET /participant/default/apply/13/1 HTTP/1.1"

I cannot figure out why this changed and how to get the code to work as POST in new location. This href execution must depend on some additional factor that I am not aware of.

You can send POST request using link thanks to JavaScript inside yii.js file that wraps it in form silently. If this JS is not loaded in assets link works in standard way which is sending GET requests.

Check if yii.js is loaded (usually through registering yii\\web\\YiiAsset directly or by dependency).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM