JavaScript post and wait for Response JSON


Jabba Hutt

Now I have the following code below, this code posts some data to my page and waits for a response with status=SUCCESS or Failure. I am trying to understand if this is asynchronous or synchronous. How can I make this JavaScript query wait for a response and then run the content inside of success? It doesn't seem to wait for a response from the content posted to.

Thanks!

my.submit = function() {
   var form = $('#lines');
   console.log(form)
   var data = form.serialize();
   console.log(data)

   $.post('', form.serialize(), function(json) {
         console.log(json)
         if(json.status === 'SUCCESS') {
               console.log('success');
               window.open(json.imgid, '_self');
        } else {
              console.log('failure');


        }
  }, 'json');

  $('#progress_bar').show();
}

I then tried to make it work the way I wanted by editing the code below, but now it only returns the HTML content of the entire page, not a JSON response. Any idea why it doesn't get a JSON response?

 my.submit = function() {
   var form = $('#lines');
   console.log(form)
   var data = form.serialize();
   console.log(data)

     $.ajax({
       url: '',
       type: 'POST',
       data: data,
       success: function(json) {
        console.log(json)
        if (json.status === 'SUCCESS') {
         console.log('Success!');
        } else {
         console.log('An error occurred.');
         console.log(data);
        }
       }
      }, 'json');


    $('#progress_bar').show();
}
Sonic Cool

dataType: 'json',add belowdata: data,

my.submit = function() {
    var form = $('#lines');
    //console.log(form)
    var data = form.serialize();
    //console.log(data)

    $.ajax({
        async: false,
        url: '',
        type: 'POST',
        data: data,
        dataType: 'json',
        success: function(json) {
          console.log(json)
          if (json.status === 'SUCCESS') {
            console.log('Success!');
        window.open(json.imgid, '_self');
          } else {
            console.log('An error occurred.');
            console.log(data);
          }
        }
     }, 'json');


    $('#progress_bar').show();
}

Related