Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Javascript Ajax success data to be accessible in another Javascript file .

Hi All,

I have two .js files: file1.js and file2.js.
In file1.js, I am able to get the data from the database using ajax call, and it is working fine.
In file2.js , I want to access the ajax success data which I received in file1.js .
How can I do that ?
Any help is appreciated.

Thanks in Advance!!
Anish Tulsyan.
 
Hi All,

I have two .js files: file1.js and file2.js.
In file1.js, I am able to get the data from the database using ajax call, and it is working fine.
In file2.js , I want to access the ajax success data which I received in file1.js .
How can I do that ?
Any help is appreciated.

Thanks in Advance!!
Anish Tulsyan.
HI there,
You would have to read in the data from file1.js as a string and convert it into a json object.
 
If you are in a browser, like using a client side Javascript, you could use localStorage or sessionStorage.
You could also use cookies or saving to user device, then read from that in other script.
X E.
 
How to import it in another file . Will simple import work . Can u pls send me a sample of working code?
Thanks
JavaScript:
var reader = new FileReader();
var content = reader.readAsText('/path/to/file');
var json = JSON.parse(content);
console.log(json);

That should work for ya
 
If you are in a browser, like using a client side Javascript, you could use localStorage or sessionStorage.
You could also use cookies or saving to user device, then read from that in other script.
X E.
Using localstorage setitem in first file , and getitem in second file I tried, but it didn't work
Can you pls send me a sample code to try.
 
On Javascript 1.
JavaScript:
localStorage.setItem("name", "data");
On Javascript 2.
JavaScript:
localStorage.getItem("name");
I recommend using sessionStorage if you are only on one page, but otherwise, feel free to keep using localStorage, but be sure to delete once you have gotten data, like this.
JavaScript:
localStorage.removeItem("name");
So your end second code is like this.
JavaScript:
let value = localStorage.getItem("name");
if (value != null) localStorage.removeItem("name");
You could just leave it but localStorage never times out so you are always getting last data.
Also, this only works if you are using same browser and tab to run Javascript 1 and 2.
If you are not using same browser, use files like Antero360 said.
If this is not working it may be a problem with your browser, and things are case sensitive so use setItem not setitem and getItem not getitem.
X E.
 
On Javascript 1.
JavaScript:
localStorage.setItem("name", "data");
On Javascript 2.
JavaScript:
localStorage.getItem("name");
I recommend using sessionStorage if you are only on one page, but otherwise, feel free to keep using localStorage, but be sure to delete once you have gotten data, like this.
JavaScript:
localStorage.removeItem("name");
So your end second code is like this.
JavaScript:
let value = localStorage.getItem("name");
if (value != null) localStorage.removeItem("name");
You could just leave it but localStorage never times out so you are always getting last data.
Also, this only works if you are using same browser and tab to run Javascript 1 and 2.
If you are not using same browser, use files like Antero360 said.
If this is not working it may be a problem with your browser, and things are case sensitive so use setItem not setitem and getItem not getitem.
X E.
This method is almost correct @anishtulsyan22... the only step that @Josiah maybe has left out is the fact whenever you store something whether it be sessionStorage or localStorage, it is usually saved as a string. You would still need to convert that string into json in order to utilize the data
JavaScript:
var value = localStorage.getItem("name");
var json = JSON.parse(value);
Granted, you still need to do proper null checking
 
Yes it is a string always and you may use a raw string, a JSON string, or any string you want, but it is always a string.
X E.
I tried the following approach :
File1:
window.sessionStorage.setItem("myresult",JSON.stringify(data));

File2:
var result = JSON.parse(sessionStorage.getItem("myresult"));
console.log("Anish");
var i;
for(i=0;i<result.length;i++){
console.log(result);
}

But this didn't worked out. I am unable to print the data in console.

Regards,
Anish
 
I tried the following approach :
File1:
window.sessionStorage.setItem("myresult",JSON.stringify(data));

File2:
var result = JSON.parse(sessionStorage.getItem("myresult"));
console.log("Anish");
var i;
for(i=0;i<result.length;i++){
console.log(result);
}

But this didn't worked out. I am unable to print the data in console.

Regards,
Anish
Because that is not how you access data in json format. you need to do something like
result.data.whatever
 
JavaScript:
var reader = new FileReader();
var content = reader.readAsText('/path/to/file');
var json = JSON.parse(content);
console.log(json);

That should work for ya
Hi,

I am passing my ajax data to a function , and in that function I am trying to write the data to a file. but I am getting the error as:
Uncaught TypeError: e.writeFile is not a function
After googling, it came to my knowledge that I can't create the file in the browser.
SO I can I create the file in the backend.
My backend is Django.
Please help.

Thanks.
 
Hi,

I am passing my ajax data to a function , and in that function I am trying to write the data to a file. but I am getting the error as:
Uncaught TypeError: e.writeFile is not a function
After googling, it came to my knowledge that I can't create the file in the browser.
SO I can I create the file in the backend.
My backend is Django.
Please help.

Thanks.
And as per your reply, before reading, I have to create the file.
Please help me on this .
 
Hi,

I am passing my ajax data to a function , and in that function I am trying to write the data to a file. but I am getting the error as:
Uncaught TypeError: e.writeFile is not a function
After googling, it came to my knowledge that I can't create the file in the browser.
SO I can I create the file in the backend.
My backend is Django.
Please help.

Thanks.
Could you post the code that produces this error?
 
To make the data from a successful AJAX request accessible in another JavaScript file, you can follow these steps:

1. Store Data in a Global Variable:In the JavaScript file where you receive the AJAX success data, store it in a global variable. This will make the data accessible throughout your application.

// In the first JavaScript file (ajax-file.js)
var aja:laugh:ata; // Declare a global variable

$.ajax({
// AJAX request configuration
success: function(data) {
aja:laugh:ata = data; // Store the data in the global variable
},
// Other AJAX settings
});

2. Access Data in Another File: In the second JavaScript file where you want to access the AJAX data, you can simply reference the global variable declared in the first file.

// In the second JavaScript file (another-file.js)
console.log(aja:laugh:ata); // You can now access the data here

By storing the AJAX data in a global variable, you ensure that it can be accessed and used across different JavaScript files within your application.

I hope this is useful.
 

Buy us a coffee!

Back
Top Bottom