Post method from ajax not working

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

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?


$.ajax({
type: ā€˜POSTā€™,
url: ā€œ/HMSLogoutā€,
data: data,
contentType: false,
processData: false,
success: function (msg) {
}

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.

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

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

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 control.

If you want to run a custom POST request, youā€™ll have to handle it manually, either by implementing a custom IDotvvmPresenter or by using other Web API framework.

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?

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?

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 :slightly_smiling_face:

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 :sweat_smile:

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.

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

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.

yeah thank you for making it work

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

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?


ajax post is not working showing as 403 forbidden