0

This question is very similar to this question (How to fill form with JSON?). The difference is that the JSON comes from a URL, and I don't know how to read the JSON returned from that URL and store in var.

URL: http://MyServer/Results.json

This call returns this JSON:

{ 
  "id" : 12,
  "name": "Jack",
  "description": "Description"
}

I only need to fill <input> description with the field "description" from the JSON stream. The example uses a loop:

<form>
  <input type="text" name="id"/>
  <input type="text" name="name"/>
  <input type="text" name="description"/>
</form>

How can this be done?

fdkgfosfskjdlsjdlkfsf
  • 3,165
  • 2
  • 43
  • 110

1 Answers1

1

I think this answer should help you: https://stackoverflow.com/a/12460434

I will just copy the solution for you...


You can use jQuery .getJSON() function:

$.getJSON('http://MyServer/Results.json', function(data) {
    //data is the JSON string
});

If you don't want to use jQuery you should look at this answer for pure JS solution: https://stackoverflow.com/a/2499647/1361042


To fill the input field you can use:

 $("#description").val("10");

but therefore you have to set id="description" to the input.


Working example

https://jsfiddle.net/f7mpoyvw/3/

In this example I'm calling the randomuser.me api which will return a random user. From the returned user the email address is written into the description field.

In your example you should then try:

$.getJSON('http://MyServer/Results.json', function(data) {
    $("#description").val(data['description']);
});

Hope this will help you!

T R
  • 565
  • 1
  • 4
  • 13