AJAX add to external file | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

AJAX add to external file

Inside JavaScript, can you append to an external file (possibly using jQuery or AJAX), and if you can, could you please tell me how, please - I am in need of knowledge.

28th Aug 2022, 7:20 PM
g1gabyte
g1gabyte - avatar
1 Answer
0
- To append to an external file using jQuery, you can use the .load() method. The .load() method takes two arguments: the URL of the file you want to append to, and a function that will be called when the file has been loaded. The function will be passed the contents of the file as an argument. For example, the following code will append the contents of the file myfile.txt to the end of the <body> element: $('body').load('basicfile.txt', function(data) { }); - To append to an external file using AJAX, you can use the XMLHttpRequest object. The XMLHttpRequest object allows you to make requests to remote servers and receive the response data. - When the request has been sent, the XMLHttpRequest object will fire the onload event. The onload event handler will be passed the response data as an argument. - For example, the following code will append the contents of the response data to the end of the <body> element: var xhr = new XMLHttpRequest(); xhr.open('GET', 'myfile.txt'); xhr.onload = function() { if (xhr.status === 200) { // The request was successful. var data = xhr.responseText; $('body').append(data); } else { // The request failed. } }; xhr.send();
30th May 2023, 9:10 AM
Sameer Mistry
Sameer Mistry - avatar