Scenario
I’m writing a web application, MVC in my case, and I need to update a specific container with the response from a get request, sometimes I want to filter the elements and find an element from the response to place in the original container.
How can I do it?
I was building a web application when I needed to partially update my content asynchronously
So I came up with a function which might suits your needs too.
Basically it will perform a get request to the url provided, it has the standard jQuery callbacks:
onSuccess,onErrorandonComplete, and you can filter() and find() on the result as well as specifying the container to place the response into.Assume you have this on your page:
And the response of the request returns this:
Now you can update it wiring the function to
myButtonclick event:Easy enough, now the function will do the rest of the work for you:
Maybe you don’t want to filter or find something in the response, so you could do:
Or maybe you don’t need any callbacks:
There you go, now you can easily update any content you want asynchronously, feel free to tweak this as needed, also you could use a request type parameter so you can GET or POST, or even adding a
loadingimage container’s id so you can display it when you enter the function and hiding it on the complete callback of the $.ajax.