An AJAX Code for Each Action(!?)

I provide many actions on my site with AJAX. For example; sending a form data, any button and etc. All these movements are in the Javascript file.

$.ajax({
		type: "GET",
		url: RootAddress + "/spine.php",
		data: "example=" + $exampleData,
		dataType: "json",
		cache: false,
		success: function (data) {
			 /* ... */
		},
		error: function (xhr, textStatus, errorThrown) {
			 /* ... */
		}

There are dozens of codes similar to the code above in a my javascript file, my JS file looks like it’s a loop. I need to create this AJAX code for each transaction. How do sites with large projects work? I think there is a method for this. It should not be like this…

Therefore I searched in the Github & GitLab site for method, but couldn’t find a useful repository.

How do you use your projects?

If they are similar, you can create functions to call them. You can pass in parameters for the differences. Anytime you are doing the same thing over and over, it is a good opportunity to abstract something into a function.

1 Like