JQuery: why $ .post does GET instead of POST

I try to make an ajax call and populate the "Data" div with the results of the ajax call, but I get the error message: "Server error in application" / ", resource not found. Description: HTTP 404. Requested URL: / Home / GetStuff" When I look with Firebug, the request was GET in / Home / GetStuff, and the response was 404 Not found. Why am I not doing POST as I needed when calling ajax? How can I do a POST?

I tried using $ .post and got the same behavior, although I did not check the jquery code, I assume that $ .post is a wrapper around $ .ajax.

I also tried Ajax.ActionLink and it works fine, although I would like to use jQuery and not the ajax js libraries for Microsoft.

The following code:

Home /TestStuff.aspx

function aClick() { $.ajax({ type: "POST", url: $("#MeFwd").href, data: ({ accesscode: 102, fname: "JOHN", page: 0 }), dataType: "html", success: renderData }); }; function renderData(data) { $("#Data").html(data.get_data()); } <div id="Data"> </div> <div id="link"> <a href="<%= Url.Action("GetStuff", "Home") %>" id="MeFwd" onclick="aClick"> Click Me!</a> </div> 

HomeController.cs

 [AcceptVerbs(HttpVerbs.Post)] public ActionResult GetStuff(int accessCode, string fName, int? page) { return "<table><tr><td>Hello World!</td></tr></table>"; } 
+1
source share
2 answers

Change onclick = "aClick" to onclick = "aClick (); return false;". By clicking on the link, you get access to the URL instead of starting JS.

+6
source
 $('#MeFwd').click(function() { $.ajax({ type: "POST", url: $("#MeFwd").href, data: ({ accesscode: 102, fname: "JOHN", page: 0 }), dataType: "html", success: function(data){ $("#Data").html(data); }); }); }; 

I don’t think you can call the function on return ... but I could be wrong.

Or look at the download: http://docs.jquery.com/Ajax/load#urldatacallback

0
source

Source: https://habr.com/ru/post/920148/


All Articles