0

I've been searching for an answer on this one, but none of the answers I found help resolving this issue:

HTTP Post via jQuery is not returning any data.

Here is the javascript code:

$.post(
    <ENDPOINT_URL>,
    <XYZ DATA SENT>, 
    function(data){
        //THIS IS THE PROBLEM HERE
    },
    "json")
    .fail(function(jqXHR, textStatus, errorThrown){
        alert(textStatus);
    });

This calls my endpoint API Url fine, which returns code 200 and a JSON response body:

{message: "XYZ"}

So here is what I've done so far:

  1. Because of the asynchronous behavior, I created a function to receive and process the data input. Nothing happens still.
  2. Added the .fail to process failures. According to an article my JSON returned may be incorrectly formatted and placing that .fail to process a failure may let me know what's going on... Is there a way to get the actual error message details?
  3. Validated my JSON. Is it incorrectly formatted or something I'm not realizing?
  4. Replaced the entire code with $ajax instead. Still, getting the same error.

I want to be able to receive that response and process the message "XYZ".

Thank you everyone for your help, much appreciated.

Tutorials/Articles I've followed:

https://api.jquery.com/jquery.post/

How do I return the response from an asynchronous call?

why cant I return data from $.post (jquery)

nukalov
  • 1,309
  • 13
  • 18

4 Answers4

1

All, thank you very much for all of the feedback - the issue is now resolved. @Austin French, after reviewing the full method on both the server side and client side javascript, I realized the issue was related to headers.

My apologies for not expanding further on my question and providing further details: I am using Amazon AWS API Gateway to process a backend Lambda function, the client calls out to the function, the function does the job and returns the JSON:

{"message":"xyz"}

I wasn't received this message on the client side using jQuery $.post. The problem came down to how AWS API Gateway processes the request and returns the response.

I needed to include the following headers as part of my Lambda's function response:

"Access-Control-Allow-Origin" : "*"

Here is the full code for the server side response:

//Response
let responseCode = 200;
var responseBody = {
    message: responseMessage
};
var response = {
    statusCode: responseCode,
    headers: {
        "Access-Control-Allow-Origin" : "*"
    },
    body: JSON.stringify(responseBody)
};

Now on the client side, my original function(data){...} receives the response and is able to process it correctly, no errors are being triggered. Client side response handler:

function(data){
    alert(JSON.stringify(data));
 }

Thanks again everyone for your help!

nukalov
  • 1,309
  • 13
  • 18
  • Interesting. Please update your tags as well to include AWS or Amazon Web Service so others can find this with similar issues. And update any answers that may have helped or proven useful even just to get you headed in the right direction – Austin T French Oct 19 '17 at 03:19
0

Try following:

$.post(
    ENDPOINT_URL,
    myData,
    function(data) {
        var response = jQuery.parseJSON(data);
        console.log(response);
    }
);
dhilt
  • 18,707
  • 8
  • 70
  • 85
  • Doesn't work :( same issue still, getting nothing from the response but in Chrome developer mode I do see the response coming in as JSON ok. – nukalov Oct 11 '17 at 02:47
0

This is the way I usually use it. It sends data as if sent in an html form and receives json response.

var value1, value2;

$.ajax({
  type: "POST",
  url: url,
  data: {"property1" : value1, "property2" : value2},
  success: function(result, status, xhr){
     <here you can process the json you get as response in result}
  },
  error: function(xhr, status, theError){
     <here process if ajax fails>
  },
  dataType: "json"
});
Juan
  • 5,525
  • 2
  • 15
  • 26
  • Doesn't work. This was almost the same ajax code I tried earlier, but still can't get anything. It does call the error code so obviously something is happening with the response that errors out, but when I do an alert(theError) I get nothing. – nukalov Oct 11 '17 at 02:49
  • If error function gets executed, then the request is failing. Check the url and obviusly if you have a connection to the url you are pointing to. Also check the developer console to see if there are any errors reported. – Juan Oct 11 '17 at 03:57
  • as noted, the request is not failing, I'm actually getting the response I expect (JSON response and 200 status code) in the developer console of Chrome, and no errors reported. Yes, the error function is being triggered, which points to an issue with the response. I'll go ahead and post the full front end and backend logs. – nukalov Oct 11 '17 at 04:12
0

A couple things that won't fit in a comment:

I prefer this format for AJAX:

var myUrl = $(selector).val();
$.post( myUrl, { name: "John", time: "2pm" })
   .fail(function (data) { /* code for failure */ }),
   .done(function( data ) {
    alert( "Data Loaded: " + data );
});

To see what the server is returning however, an easy thing to do is use Chrome's debugger:

Chrome Debugger Console

Go to the network tab, choose the calling method and then on the right sub pane choose response. You should be able to see not only the response code, but the full contents returned.

A couple additional notes:

the .Done will call the function once the AJAX completes.

Depending on Jquery version you might not have not have a .done but rather .success and vice versa.

Austin T French
  • 5,022
  • 1
  • 22
  • 40
  • Thank you. I did use this already with Chrome, my status code is 200 and the response is {"message":"xyz"} which is a properly formatted JSON string. – nukalov Oct 11 '17 at 02:50
  • Also, I did also use the .done before, still nothing. – nukalov Oct 11 '17 at 02:52
  • @nukalov then it appears you are getting a successful POST response and a valid JSON, so define the .done or .success event to do something with it. – Austin T French Oct 11 '17 at 02:52
  • @I'd post the full method then in your question with done, and also confirm your exact version of jQuery – Austin T French Oct 11 '17 at 02:58