Search code examples
javascriptjsonajaxpostresponse

Get response data from post request in ajax/jquery


I'm building a website and server and I need to get some data from my server. I made the server so that I have to post a json string with a lightID and a string. The response I should get is in json format. I know the response from the server is working because I can see it in the developers console in chrome. There I can look at the XHR data and see in the response tab the json I expected. I need to know how I can pass this response into another function on the website to use the data.

If someone knows the answers it would be really helpfull. The answers I found while googling didn't work for me or I didn't understand them right...

function dataNew(){
        request = {lightID: 'livingroom'};
        url = "data"
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'post',
            contentType: 'application/json',
            data: JSON.stringify(request)
        });
        console.log("dirk")

    }

Solution

  • Try:

    $.ajax({
        url: url,
        dataType: 'json',
        type: 'post',
        contentType: 'application/json',
        data: JSON.stringify(request)
    })
        .done(
           function(response) { ... your code ... }
        );