0

I have pages that send POST/GET requests to PHP scripts on the server. All PHP scripts respond in JSON. Question is how to capture the JSON response at the client-side in JavaScript.

Example : when i submit the form register.html, i want to capture and manipulate (using Javascript) the JSON response returned from http://localhost/register.php.

3 Answers 3

2

You have to make an AJAX request. You can do this quite simply by using a library such as jquery. Or a little more difficultly just using javascript.

Using AJAX will change the current flow of your application though. This follow example is using jquery

<form onSubmit="makeRequest(); return false;"></form>

function makeRequest() {
  $.post('register.php', formDataHereAsAnObject, function(response) {
     console.log(response) // this response is your json
  });

}
Sign up to request clarification or add additional context in comments.

Comments

0

i could recommend http://www.json.org/js.html for a detailed description of using JSON.

Comments

0

The moment you submit a form in the classic sense, you're out of luck. What you want is to load the JSON response from the server. To achieve this, there are a few possibilities

  • Set the target of your form to an invisible iframe and do submit it, then take the JSON out of there via JS (old school)
  • Start an AJAX request (via a framework or directly), that ends up having your data in a JS variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.