-2

This could be a basic question but , i'm stuck with this for some time.

i have a data

{pid: 'WEB506', session: 'WEB506_09092021_M1_S1.csv'} 

how to convert this to

{ "pid":"WEB506", "session":"WEB506_09092021_M1_S2.csv" }; 

regards. i used JSON.stringify and i got it as

{"pid":"WEB506","session":"WEB506_09092021_M1_S1.csv"} 

but when i pass this to ajax data to call an api , the value entering req.body is

{ '{"pid":"WEB506","session":"WEB506_09092021_M1_S1.csv"}': '' } 

This is my ajax call

function graphApi(){ const apiValue = JSON.parse(localStorage.getItem('user')); console.log(apiValue,"apiValue"); const f = JSON.stringify(apiValue); console.log(f,"ffff") $.ajax({ type: "POST", data: f, url: "http://localhost:5000/File", success: function (data) { console.log(data,"graph api"); } 

I really don't know what is happening.

regards

8
  • How are you passing the ajax data? Did you try passing the object as-is? Commented Nov 1, 2021 at 9:37
  • passing the data as a string. Commented Nov 1, 2021 at 9:38
  • 2
    provide code for ajax code Commented Nov 1, 2021 at 9:41
  • @ Ravi Ashara please check this question stackoverflow.com/questions/69565573/… Commented Nov 1, 2021 at 9:44
  • 1
    yes @SmitGajera Commented Nov 1, 2021 at 9:50

3 Answers 3

0

JSON.stringify() is used to stringify the objects

Sign up to request clarification or add additional context in comments.

Comments

0

This is your Object Right

 const object = { "pid":"WEB506", "session":"WEB506_09092021_M1_S2.csv" }; 

Now Pass this object in the below method

const jsonObject = JSON.parse(object); console.log(jsonObject) 

Now you can dig out your PID or SESSIONS with

const pid = jsonObject.pid const session = jsonObject.session 

2 Comments

If you are getting this object from the body you can directly parse it from JSON.parse(req.body)
bro what i have is this {pid: 'WEB506', session: 'WEB506_09092021_M1_S1.csv'} , which i got as {"pid":"WEB506","session":"WEB506_09092021_M1_S1.csv"} when i use JSON.stringify. and i pass this stringified data to ajax , which gives { '{"pid":"WEB506","session":"WEB506_09092021_M1_S1.csv"}': '' } in my req.body in api
0

JSON.stringify()

should give you serialized string for your data.

I think the issue here is you are passing the string as the payload to the AJAX call whereas you should use the data(i.e the object) directly in req. body. See if this helps

1 Comment

i did this but no hope

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.