0

I want to create a team and POST it to my database through the REST service i created, which is working. BUT i need the id in return becuase i have to add it in another POST request, so what is the best way to get auto incremented id in return? am i missing some easy solution?

/*Post Base Request */
function postAjax(url, data, success) {
    var params = typeof data == 'string' ? data : Object.keys(data).map(
            function(k){ return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) }
        ).join('&');

    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    xhr.open('POST', url);
    xhr.onreadystatechange = function() {
        if (xhr.readyState>3 && xhr.status==200) { success(xhr.responseText); }
    };
    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(params);
    return xhr;
}

/* Funtion used onClick "create team" */ 
function createTeam(){
    var teamToSend = {
        teamName: document.getElementById('name').value
    }
    const convertTomemberIdString = JSON.stringify(teamToSend);
    postAjax('https://xxx.azurewebsites.net/Service1.svc/teams',convertTomemberIdString, function(data){ console.log(data);})
    console.log(`${status}`);
}
  • There's no "ajax way" to do this; you have to [determine the id on the server](https://stackoverflow.com/questions/15821532/get-current-auto-increment-value-for-any-table) and add it to your reply. –  May 16 '19 at 10:50

1 Answers1

0

Two options:

  1. Have it returned from your server as a response to this request.
  2. Maintain a counter variable, and increment it when the request is successful. (Not persistent as you'll lose the value on page refresh)
Sujit Y. Kulkarni
  • 1,186
  • 3
  • 22
  • 37