How to put ajax request inside function and call it when necessary?

markusman

I have an ajax request:

$.ajax({
type: 'POST',
url: '/get-result.php',
dataType: 'json',
data: 'pid=' + $(this).attr("id"),
success: function(response) {
$(".reviewee-fname").append(response['fname']);
$(".reviewee-lname").append(response['lname']);
    }     }); };

I want to be able to put this inside a function that waits for me to trigger it with a return call. I am not exactly sure how to word it, I am new to javascript and jquery. But basically, I want to trigger this ajax call with various different button clicks and instead of having to put the ajax call inside every button click event, I want to put it in a stand alone function so if I ever update it later I dont have to change it 5 times.
Heres an example of a click event Id like to call the ajax request function with. Thanks!

$(function() {
$(".task-listing").click(function() {
//Call Ajax function here.
});
});
Zain Ul Abideen

Callbacks are well-suited for this scenario. You can encapsulate your ajax call in a callback function.

function apiCall() {
$.ajax({
type: 'POST',
url: '/get-result.php',
dataType: 'json',
data: 'pid=' + $(this).attr("id"),
success: function(response) {
$(".reviewee-fname").append(response['fname']);
$(".reviewee-lname").append(response['lname']);
    }     }); };
}

You can now hook apiCall()method as a callback to button click.

$(function() {
$(".task-listing").click(apiCall);
});

By doing this you will able to achieve this.

I want to put it in a stand alone function so if I ever update it later I dont have to change it 5 times.

EDIT:

Note:

This is lead to start, you can alter this according to your requirement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to call function inside ajax call

How to call the function inside the api request function?

How to access a javascript function inside an ajax request

how to call autocomplete inside ajax function

How to call medoo 1.5 inside a function with ajax?

Fetch request inside function. How to put it in ComponentDidMount?

ajax doesn't work properly when call inside a function

Ajax Call Breaks When executed inside a function and throws a warning

How to call a function when using ajax promise

Put result of ajax request inside div

Jquery ajax call inside a then function

Ajax request, how to call an other function than "success" one's?

How to fix the ajax request to call a function in php file?

How to recursively call a function 1 second after an ajax request is done?

How to make jquery ajax request to call function ChangeLoginStatus?

in jquery how to call a function after an ajax request has been sent?

Why is a PUT request not sent by my function when I call another function to refresh the window?

R how to get the function definition when call inside other function

Is it possible to put the ternary operator inside a function call?

Is is necessary to have success as a parameter in Ajax call if my function is not returning anything?

The arrow function does not work when I put it inside the object, the result after call is NaN

How to fix ajax call request

How to call function when space key is pressed inside the lineEdit

How To Call A Javascript Function From Inside Jquery When An ID is Clicked

Call function inside the Axios request (then) [Vuejs]

Simplify list comprehension when a call to function returning a tuple is necessary

Pause function to wait for ajax request inside of a websocket

Why is `trap -` not working when put inside a function?

function not working when put inside Vue methods