jquery - turn a curl call into a javascript ajax call -


how turn curl request ajax call in javascript? (an answer in raw js or library fine)

curl:

curl -h "content-type: application/json" -d '{"foo":0, "bar": 0, "baz":"test"}' -x http://localhost:8080/public/v1/state/helloworld

the url tried in ajax call (it gives 404 error):

get http://192.168.56.101:8080/public/v1/state/helloworld?foo=0&bar=0&baz=test

code

return axios.get(switchdomain + '/public/v1/state/helloworld', {     params: {       foo: 0,       bar: 0,       baz: "ber",     }   })   .then(function(response){     console.log('perf response', response);   }); 

seems -d turn curl request post regardless of -x option, so:

var req = new  xmlhttprequest(); req.open( "post", "http://localhost:8080/public/v1/state/helloworld", true); req.send("foo=0&bar=0&baz=test"); 

eventually may need add content-type header after req.open , before req.send.

req.setrequestheader("content-type", "application/x-www-form-urlencoded"); 

or posted in question, may want send json

var req = new  xmlhttprequest(); req.open( "post", "http://localhost:8080/public/v1/state/helloworld", true); req.setrequestheader("content-type", "application/json"); req.send(json.stringify({     foo: 0,     bar: 0,     baz: "test" })); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -