2

How do I make a call to a REST Api with Javascript (or ajax or jquery)?

curl -v -X PUT -H "Content-Type: application/json" -H "Accept: application/json" -X PUT --user user:password http://url -d "{\"name\": \"Marcus0.2\",\"start\": 1361381326000,\"end\": 1361640526000}"

I need help converting this to a useable ajax call. Thanks

$.ajax({
    type: 'put',
    url: 'https://this.is.my/url',
    dataType: 'json',
    data: { \"name\": \"Marcus0.3\" , \"start\": 500000 , \"end\": 1361640526000 }
    });

This code does not work [not sure why]. Also, 'https://this.is.my/url' is just short for the longer url, which I don't want to post online

4
  • Hmmm. Are you set against using jQuery? jQ sure makes it easy via $.ajax(). Commented Aug 5, 2013 at 21:03
  • Nope, I didn't know it was a possibility. Question updated Commented Aug 5, 2013 at 21:04
  • OK, just check out jQuery and the $.ajax() call. Commented Aug 5, 2013 at 21:05
  • I did, but I struggling to get it to work. I'll update the question with my current code Commented Aug 5, 2013 at 21:06

1 Answer 1

2

No need to add escaping to your data element... you can send a whole object and jQuery will take care of it for you.

$.ajax({
  type: 'put',
  url: 'https://this.is.my/url',
  dataType: 'json',
  data: {
    name: 'Marcus0.3', 
    start: 500000,
    end: 1361640526000 
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Followup question: This call returns a dictionary. How can I save/access this dictionary? thanks
@IdeoREX, $.ajax() returns a promise, which you can use to add a callback to handle when the request is done. This callback will get the data in an argument. $.ajax(/* ... */).done(function (data) { console.log(data); }); See the example near the bottom of the documentation page: api.jquery.com/jQuery.ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.