Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .load() function is hitting my <HttpPost()> action instead of my <HttpGet()>

I have an overloaded action in my method, one declared with and the other with .

I use the Post metod to handle my form's submit button, which works fine.

I wanted the HttpGet method to handle a jQuery .load() action but that is instead also being caught by my Post method.

Any idea what I'm missing? Do I have to explicitl call a .get() or .ajax() to hit the right action?

Thanks!

-Ben

like image 428
Ben Finkel Avatar asked Jul 27 '11 21:07

Ben Finkel


People also ask

How to load function in jQuery?

jQuery load() Method The load() method loads data from a server and puts the returned data into the selected element. Syntax: $(selector). load(URL,data,callback);

Is jQuery load asynchronous?

You can use jQuery to support both synchronous and asynchronous code, with the `$.

How do I only load part of a page in HTML?

Method 1: Using HTML: One can use the anchor tag to redirect to a particular section on the same page. You need to add ” id attribute” to the section you want to show and use the same id in href attribute with “#” in the anchor tag.

Which function is used to load a resource in the background in ajax?

The browser performs a JavaScript call to the Ajax engine. In other words, create an XMLHttpRequest object. In the background, an HTTP request is made to the server and the appropriate data is retrieved. HTML, XML, or JavaScript data is returned to the Ajax engine which then delivers the requested data to the browser.


1 Answers

The .load method could send an HTTP POST AJAX request as stated in the documentation:

The POST method is used if data is provided as an object; otherwise, GET is assumed.

For example

$('#result').load("/foo", { id: 123 }, function(result) {

});

will send a POST request.

If you want to be sure use $.get, or $.ajax with type: 'GET'. Also don't forget that if you use GET request for AJAX some browsers might cache the results and get you into trouble or at least some weird behavior, so if you want fresh contents from your server use $.ajax with cache: false parameter.

like image 162
Darin Dimitrov Avatar answered Nov 15 '22 07:11

Darin Dimitrov