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
- it was working before and we have migrated from old framework and that code wasnāt changed
- 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
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.
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?
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;
}
@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
this is another example that is common in webforms:
JQuery Ajax Post to C# - Stack Overflow
how do you translate that into the DotVVM world?