-2

I have the following information:

URL: POST https://api.recman.no/post/

Type: JSON

POST /post/ HTTP/1.1

Content-Type: application/json

{
  "key": "YOUR_KEY",
  "scope": "candidate-import",
  "data": {
    "corporationId": 2,
    "firstName": "Johnny",
    "lastName": "Bravo",
    "title": "CTO"
  }
}

What is the best way to write a Ajax post request with this using jQuery?

API spesific documentation can be found at https://help.recman.no/help/candidate-import-via-api/ and jQuery.ajax() documentation at http://api.jquery.com/jquery.ajax/

UPDATE 1:

The following code just gives me "error". (Key is taken out for security reasons)

data = {
  "key": "**********",
  "scope": "candidate-import",
  data: {
    "corporationId": 2,
    "firstName": "Johnny",
    "lastName": "Bravo",
    "title": "CTO"
  }
}

jQuery.ajax({
  type: "POST",
  url: "https://api.recman.no/post/",
  contentType: "application/json",
  dataType: "json",
  data: data,

  success: function(data, status, jqXHR) {
    alert(status);
  },

  error: function(jqXHR, status) {
    // error handler
    alert(status);
  }

});
iamchriswick
  • 380
  • 2
  • 8
  • 24
  • 1
    Possible duplicate of [Jquery Ajax Posting json to webservice](http://stackoverflow.com/questions/6323338/jquery-ajax-posting-json-to-webservice) – Igor Apr 07 '16 at 13:04
  • Did you look at the jquery post docs? What are your issues when you try to follow the example in the documentation? – epascarello Apr 07 '16 at 13:12
  • If I knew how to write it, I wouldn't have asked the question. Where does my json example fit into the `$.ajax({});` function? – iamchriswick Apr 07 '16 at 13:19
  • Did you look at that other question at all? You could copy paste the [answer](http://stackoverflow.com/a/6323528/1260204), change the data and the URL and it would work (to the extend that it addresses your question, no the actual structure of the data required by whatever API you are calling). – Igor Apr 07 '16 at 13:24
  • @Igor I did, but my json example is not the same as in that question, so I would not know what to change. – iamchriswick Apr 07 '16 at 13:28
  • @iamchriswick - so you can't copy paste and then replace the data with your data? If you can and it does not work then update your question with what you have tried so far (that copied/pasted code plus your data) and the error message you are then experiencing. It is not realistic for you to expect the community to write the code for you, only to help/guide you to finding a solution. – Igor Apr 07 '16 at 13:30

2 Answers2

0

It should be enough using the JSON.Stringify() to parse the object.

Erin_O
  • 107
  • 11
0

try this

var sendData = {"key": "YOUR_KEY","scope": "candidate-import","data":{"corporationId": 2,
"firstName": "Johnny",
"lastName": "Bravo",
"title": "CTO"}};
$.post('', sendData , function(result){
if(result == x )
alert('your request was succesfully done ');
else{
alert(result)}
})
/*that "x" is the return of your query it may be boolean as true or a value : 1 for example*/

and in your php file you made a simple test on a value submitted in that post let's take for example the "title" and you type

if(isset($_POST['title']) && !empty($_POST['title'])){
echo "1";
}else{
echo "0";
}

hope it was helpful ah just to not forget here i supposed that the php file is with the same JS file containing the request so for that the first parameter in $.post was empty if it is not you should specify the url of the php file there .

PacMan
  • 1,358
  • 2
  • 12
  • 25
  • Where would I then put scope and key? – iamchriswick Apr 07 '16 at 13:24
  • i have just noticed that and it was edited , you let me know if that was helpful or not and for the php test you can test for any value submitted in the post not only title. – PacMan Apr 07 '16 at 13:32
  • And then the post URL goes like this: `$.post('https://api.recman.no/post/', sendData`? – iamchriswick Apr 07 '16 at 13:39
  • yes absolutly this is the url who will receive your request as POST and treat it – PacMan Apr 07 '16 at 13:42
  • A successful response will be in the format of `{"success": true}`, so how would you recommend that I check for this with your code? – iamchriswick Apr 07 '16 at 14:03
  • you can just alert the result in all cases coming from your request like this $.post('', sendData , function(result){ alert(result); }); and let's suppose the alert show "true" , then you made a simple test on it . hope that was your helpful to your question , else i'll need more details – PacMan Apr 07 '16 at 14:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108533/discussion-between-rmidi-ayoub-and-iamchriswick). – PacMan Apr 07 '16 at 14:22