Hey Friends, hope I’m posting this to right category. I have question that I’m sure of you will find quite easy to answer while other’s struggle to learn. In short, I need to process the response from a form post. Should I post this to a remote server using JSON and process the response in JSON? I just want to send up a dialog box when the end user clicks the send button according the response, which seems to come back in JSON.
Depends on what the server is expecting. The fact that you are asking this question makes me believe that your intention is to use AJAX to post the data to that URL in your form action without actually sending the browser there. Correct?
depends on what the server sends back.
I cannot intuit from the form action what it’s expecting you to do, nor what its response will be.
I’m not even sure if this is possible after my reasearch, but… What I am trying to do is send a POST request to my PBX, which works great, then I receive what appears to be a response back in jason, which is:
The server typically gives back the content but also a set of headers. In the headers you will often get a “Content-Type” header that would be set to “application/json” and you can certainly read that and know you have some JSON and then parse it.
Hey Marty, I’m pretty sure it feeds something back in the header, but POSTS are hidden no? Unlike GETS which aren’t quite hidden from view? Maybe I can use Firefox Developer tools to find it?
No no… the response is for the page, nothing to do with the dom. You can get the header from the response and do things with it. Read up on the Javascript I showed you. When the page is given to your browser, JS reads the headers, you can then do whatever you need to with the page.
Marty do I have to execute the script as part of the POST process or do I just call the script in tags? Sorry, confused here, always a block the first time trying something new.
Dennis, how to do I read that in though? Am I using the DOM for that? Is there another method to do this? Once I read it in, I just want a pop up based on the response - if it’s a success, throw up a success message, otherwise a failed message.
The idea is to post a phone number to a PBX system - which it does. The response back could be a success, fail, or server busy. I just want a pop up on the success condition which is returned from the PBX Server as such:
So what Zensei is doing is chaining. x.functionOne().functionTwo()
is essentially the same as
let result = x.functionOne();
result.functionTwo();
fetch will post the data to the API. This is the most common way of performing what’s called an AJAX (Asynchronous Javascript and XML, if you care) request, which generates another HTML request from the browser to the target resource. then tells it “When you get a response from the thing before me, (then) do this with it.”
the first then tells the result to be parsed as a JSON object.
the second then (which is waiting for the first then’s result, because chaining) takes the result (which is now a json object), and acts upon it. catch is there so that if any part of the chain collapses and throws an error, we can see the error generated.
AJAX is how a lot of things work - like this forum. You can see in real time who’s writing a post, and when new posts appear… that’s a whole lot of AJAX requests going back and forth between your browser and the server to check on the status of the thread.