Reputation: 656
I can't figure this out :-( My Client is doing an ajax httppost and I need the following to be sent over:
private doSaveQuery(queryName: string): void {
const info = {};
info['userName'] = 'admin';
info['userQueryName'] = queryName;
...
When it gets stringified it turns it into this:
{"userName":"admin",
"userQueryName":{"queryName":"MyName"}
}
Why does it take my variable name "queryName" and adds it as a key to my value?????
How do I get to this?
{"userName":"admin",
"userQueryName":"MyName"
}
I know this is something simple so please forgive my ignorance :-)
Thanks!
Upvotes: 0
Views: 59
Reputation: 1532
You specified as a type string, and typescript probably should've caught that you're not passing a string. Because if you do, it would do what you want. But probably happens something like this.
function getMyJSON(queryName) {
const info = {};
info['userName'] = 'admin';
info['userQueryName'] = queryName;
return info;
}
const a = {queryName:"name"};
console.log(JSON.stringify(getMyJSON(a)));
Upvotes: 1
Reputation: 6058
When you are encoding the object it looks like queryName
is actually an object that looks like {queryName: 'MyName'}
.
To fix this you should make sure that queryName
is just the string MyName
.
info['userQueryName'] = 'MyName'
In order words, queryName
is not what you think it is.
Example of your current implementation:
const queryName = {queryName: "MyName"}
const info = {
userName: "admin",
userQueryName: queryName
}
console.log(JSON.stringify(info))
You should change queryName
to just the string:
const queryName = "MyName"
const info = {
userName: "admin",
userQueryName: queryName
}
console.log(JSON.stringify(info))
If you can't do then, then you could build info from the property:
const queryName = {queryName: "MyName"}
const info = {
userName: "admin",
userQueryName: queryName.queryName
}
console.log(JSON.stringify(info))
Upvotes: 1