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