# Post method from ajax not working

**URL:** <https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89>\
**Category:** Discussion in English\
**Created:** [November 17, 2023, 4:18pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89 "2023-11-17T16:18:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [November 17, 2023, 4:18pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/1 "2023-11-17T16:18:35Z")

</div>

Failed to load resource: the server responded with a status of 405 (Method Not Allowed). It is happening in 4.2 version

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [November 19, 2023, 11:06am UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/2 "2023-11-19T11:06:33Z")

</div>

Hello Daniel, I’m sorry but I cannot help you without more details about the kind of request which is failing. Could you please share the request details (in browser devtools, see Network tab, you can send us screenshot or copy the request as curl). Does the response has any body?

---

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [November 19, 2023, 1:35pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/3 "2023-11-19T13:35:30Z")

</div>

![image](https://forum.dotvvm.com/uploads/default/original/1X/787e5a770cdc188004b3b509db49756c7d4d6e9d.png)  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/b2e35ce8207785f4181e8efc243395ad370eb564.png)

$.ajax({  
type: ‘POST’,  
url: “/HMSLogout”,  
data: data,  
contentType: false,  
processData: false,  
success: function (msg) {  
}

---

<div class="post-metadata">

**Author:** ![tomasherceg](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/tomasherceg/32/513_2.png) [@tomasherceg](https://forum.dotvvm.com/u/tomasherceg)\
**Post date:** [November 19, 2023, 3:01pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/4 "2023-11-19T15:01:15Z")

</div>

How is the page `/HSMLogout` implemented? Is it a DotVVM pager or some MVC controller?  
If it is a DotVVM page, you cannot call it via jQuery.ajax.

---

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [November 19, 2023, 4:06pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/5 "2023-11-19T16:06:02Z")

</div>

Yes it is a dotvvm only, then how can I handle it, via fetch function?

---

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [November 20, 2023, 6:54am UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/6 "2023-11-20T06:54:37Z")

</div>

Pls let me know how to handle it, bocz in some pages we have used ajax only to call dotvvm pages

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [November 20, 2023, 10:02am UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/7 "2023-11-20T10:02:35Z")

</div>

I don’t understand why are you trying to run a POST request to `/HMSLogout` from `/Default` page. Are you trying to run a command from a different page? I’d advice against this, commands and staticCommands only should be run against the same URL; if you want to run a command from JS, you can use the [NameCommand](https://www.dotvvm.com/docs/4.0/controls/builtin/NamedCommand) control.

If you want to run a custom POST request, you’ll have to handle it manually, either by implementing a [custom IDotvvmPresenter](https://www.dotvvm.com/docs/latest/pages/concepts/routing/custom-presenters?tabs=aspnetcore) or by using other Web API framework.

---

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [November 20, 2023, 10:23am UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/8 "2023-11-20T10:23:19Z")

</div>

In my case it is same url only. We have migrated it from .net to .net core using dotvvm, these are old functions which was working till now I mean till 4.1, is there any reason for restricting it?

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [November 20, 2023, 11:45am UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/9 "2023-11-20T11:45:36Z")

</div>

Ok I didn’t get it worked on the previous version, we may have broken something accidentally. However, I still don’t understand what exactly are you doing. At least in the screenshot, it’s two different URLs.

When running a command, DotVVM expects a `X-DotVVM-PostBack` and POST method. When executing a page normally, DotVVM expects a GET request (no headers required, but `X-DotVVM-SpaContentPlaceHolder` can be used to switch to SPA mode). From looking at the changes we made around this code, DotVVM \<4.2 maybe allowed POST requests without this header and treated them like a GET request. Are you relying on this interesting behavior?

If your goal is to provide a logout button on each page, I suppose you could simply call the logout function from a staticCommand, right?

---

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [November 20, 2023, 12:04pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/10 "2023-11-20T12:04:06Z")

</div>

Yes what we do is call the logout page using ajax which clears the session and in success event we redirect to login page.  
I have raised the question for 2 reasons

1. it was working before and we have migrated from old framework and that code wasn’t changed
2. If this the only page I have to change then I would see different solution but the ajax post was used in many pages, that why asked

anyway I’ll update the code 🙂

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [November 20, 2023, 1:21pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/11 "2023-11-20T13:21:39Z")

</div>

So you are using plain POST requests to run DotVVM pages? Do you also parse the resulting HTML on the client? How do you handle the request body on server, do you just parse the HttpContext.Request.Body manually?

Anyway, migration from .NET Framework to .NET “Core” probably does not play a role in this, I guess it will work on DotVVM 4.1 on AspNetCore. If you do this on many places, I’d discuss this with Tomas and we may revert the change. We didn’t change it intentionally, but I think it was a mistake that it used to work in the first place 😅

Alternatively, I think that you can workaround it by putting an AspNetCore middleware before DotVVM which rewrites `POST` to `GET` (should be just `context.Request.Method = “GET”) so that DotVVM is happy and you can parse whatever you need from the request body.

---

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [November 20, 2023, 1:36pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/12 "2023-11-20T13:36:52Z")

</div>

Alright, we don’t play with html we used ajax post for uploading images in all pages and logout, other things are handled using dotvvm commands. Developers who worked on old framework used ajax post as it was convenient. You can revert if it is not a issue otherwise I’ll change from my side

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [December 15, 2023, 1:50pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/13 "2023-12-15T13:50:22Z")

</div>

Hi, sorry I forgot to let you know, we added back the support for plain POST requests. Your code should work on the latest patch of 4.2.

---

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [December 18, 2023, 12:39pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/14 "2023-12-18T12:39:19Z")

</div>

yeah thank you for making it work

---

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [February 15, 2024, 7:04am UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/15 "2024-02-15T07:04:51Z")

</div>

I have checked by installing 4.2.5, still facing the same issue

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [February 16, 2024, 6:03pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/16 "2024-02-16T18:03:10Z")

</div>

Ok, I’m sorry, then I most likely do not understand the issue and we fixed something different. Would you please be able to recreate the same problem in a project you could share with us?

---

<div class="post-metadata">

**Author:** ![Danielajithraja](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/danielajithraja/32/52_2.png) [@Danielajithraja](https://forum.dotvvm.com/u/Danielajithraja)\
**Post date:** [February 19, 2024, 5:30am UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/17 "2024-02-19T05:30:25Z")

</div>

![image](https://forum.dotvvm.com/uploads/default/original/1X/1473c2fd8973014c0c8876dd7d158f8c3707cdb1.png)  
ajax post is not working showing as 403 forbidden

---

<div class="post-metadata">

**Author:** ![scoachtom](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/scoachtom/32/41_2.png) [@scoachtom](https://forum.dotvvm.com/u/scoachtom)\
**Post date:** [July 1, 2024, 6:45pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/18 "2024-07-01T18:45:52Z")

</div>

hi there,

I have the same issue. giving me also the 403 forbidden.  
Is there a cheat code for DotVVM how to do this clasic AJAX???

```
            $.ajax({
                type: "POST",
                url: "SITEURL.COM/functionname",
                data: JSON.stringify({
                    a: "123",
                    b: "123,
                }),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (response) {.........................

```

how do you retrieve the data and givve back the reponse in ViewModel?

in webforms it was working like this:

[WebMethod]  
public static string functionname(string a, stringb)  
{  
return json;  
}

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [July 1, 2024, 8:53pm UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/19 "2024-07-01T20:53:02Z")

</div>

@scoachtom Sorry, I don’t really understand the question, could you please share more details on what are you trying to accomplish?

DotVVM does not do anything special with regard to your JavaScript requests, the 403 thus probably coming from your handler of `/functionname`. Is there more details in the logs or in the response body?

You can read the view model from `dotvvm.state` and modify it using the `dotvvm.setState`, `dotvvm.patchState` and `dotvvm.updateState` functions. However, if you are simply trying to invoke a method on the server and store its result in the model, maybe you can just use a `command` or a `staticCommand`

---

<div class="post-metadata">

**Author:** ![scoachtom](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/scoachtom/32/41_2.png) [@scoachtom](https://forum.dotvvm.com/u/scoachtom)\
**Post date:** [July 2, 2024, 6:21am UTC](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89/20 "2024-07-02T06:21:22Z")

</div>

this is another example that is common in webforms:

[JQuery Ajax Post to C# - Stack Overflow](https://stackoverflow.com/questions/10653637/jquery-ajax-post-to-c-sharp)

how do you translate that into the DotVVM world?

[Next page](https://forum.dotvvm.com/t/post-method-from-ajax-not-working/89.md?page=2)
