I’m trying to use XMLHttpRequest to send a PUT request but I’m not sure how to pass arguments.
The curl version of what I want to send is :
$ curl -u me@myurl.com -X PUT -d 'data={"keyname":"keyvalue"}' https://api.myurl.com/v1/action
I’ve got an auth token to make calls so what I’m trying is this:
var xhr = new XMLHttpRequest();
xhr.open("PUT", ROOT_URL + link + "?auth_token=" + token, true);
xhr.setRequestHeader("Cache-Control", "no-cache");
var data = 'data={"keyname":"' + keyvalue + '"}';
xhr.send(data);
But that doesn’t seem to be correct because I get a 500 server error back which says
"data={\"keyname\":\"keyvalue\"}" does not have a valid root.
Any ideas on what I’m doing wrong?
That is because
"data={\"keyname\":\"keyvalue\"}"does not represent a valid json object.Try using
Or