Orchardcore: problem in run action by jquery

Created on 20 Aug 2019  路  8Comments  路  Source: OrchardCMS/OrchardCore

i wanna call the action from my module by jquery, but it can not run the method and i get " Bad Request 400 Bad Request" message.

i have this method:

namespace Contact.OrchardCore.Controllers
{ 
    public class HomeController : Controller, IUpdateModel
    {  [HttpPost]
        [Route("SubScription")]
        public Boolean SubScriptionpost(  )
        {}}}

this jquery:

  $(document).ready(function () {
            $("#btnnewsletter").click(function (e) {

                    $.ajax({
                        type: "POST",
                        url: "/SubScription",
                        contentType: "application/json; charset=utf-8", 
                        dataType: "html",
                        success: function (result, status, xhr) {
                            alert("thanks"); 
                        },
                        error: function (xhr, status, error) { 
                            $("#dataDiv").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
                        }
                    });

                return false;
            });
        });

question

All 8 comments

I think the URL is not the right one. If you want /SubScription as a url, you need to map that route to your controller in the Startup.cs of your module

Also it would not work if you are in a prefixed tenant (not the default one) and / or under a virtual folder.

In that case, e.g in a razor view, you would have to init e.g a data attribute with the value returned by an url action, and then retrieve this generated url in your script.

Anyway i think it's better to do it this way.

Also it would not work if you are in a prefixed tenant (not the default one) and / or under a virtual folder.

In that case, e.g in a razor view, you would have to init e.g a data attribute with the value returned by an url action, and then retrieve this generated url in your script.

Anyway i think it's better to do it this way.

I'm using the default, it's in my theme module. and I'm using a liquid layout. any solution to get url in liquid layout?

when i copy method url it calls the method:
https://localhost:5001/SubScription

but in jqury, it doesn't work and i got error alert.

$.ajax({
type: "POST",
url: "https://localhost:5001/SubScription",
contentType: "application/json; charset=utf-8",
data: '{"email":"' + $("#email").val() + '" }',
dataType: "html",
success: function (result, status, xhr) {
alert("thanks");
},
error: function (xhr, status, error) {
alert("error");
$("#dataDiv").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
}
});

I think you need an antiforgery token, normally in a form it is auto generated otherwise i think there is an helper to generate it yourself.

<input name="__RequestVerificationToken" type="hidden" value="CfDJ8F..." />

Then in your script you have to retrieve it an use it

function moveNode(AdminMenuId, nodeToMoveId, destinationNodeId, position) {
    var url = "@Url.Action("MoveNode", "Node", new { area = "OrchardCore.AdminMenu"})";
    $.ajax({
        url: url,
        method: 'POST',
        data: {
            __RequestVerificationToken: $("input[name='__RequestVerificationToken']").val(),
           ...

There are some examples in our *.cshtml / *.js files

@jtkech unfortunately, it doesn't works. I'm using liquid layout theme. so i can not use "
var url = "@Url.Action("MoveNode", "Node", new { area = "OrchardCore.AdminMenu"})";
my jquery script in sparated file named main.js

when i set method type to GET, it calls the action but doesn't send the value to the parameter. "emailsub" it will be null.

    [Route("SubScription")]
        public Boolean SubScription(string emailsub)
        {

There is a liquid tag for that:

https://orchardcore.readthedocs.io/en/dev/OrchardCore.Modules/OrchardCore.Liquid/#antiforgerytoken
@sebastienros
sorry, but it still doesn't call the post action. just it calls get action and can don't send the parameter to action.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jeffolmstead picture jeffolmstead  路  4Comments

khoshroomahdi picture khoshroomahdi  路  4Comments

lzw5399 picture lzw5399  路  3Comments

hishamco picture hishamco  路  3Comments

randaratceridian picture randaratceridian  路  3Comments