ShakC
ShakC

Reputation: 53

how to parse json URL data in HTML web page

I want to pare the JSON URL data in HTML web page and display it on page,I am using following code.

I want to show JSON Data in paragraph div.

<script>
  function gContent(){
    alert('working');

    $.getJSON('http://www.celeritas-solutions.com/pah_brd_v1/productivo/getGroups.php?                 organizationCode=att&userId1&', function(data) {
       alert(data);
       $(".paragraph").html(data);
    });
  }
</script>

Upvotes: 1

Views: 3002

Answers (5)

Ganesh Babu
Ganesh Babu

Reputation: 3670

 Origin null is not allowed by Access-Control-Allow-Origin. 

This is the message one usually get when trying to get data from other websites/domains due to same origin policy. Inorder to overcome this problem we go for two major methods:

You can easily get your data using JSONP. If needed, you can use CORS, where you need that other website to allow your site in its headers.

In JSONP callback, one should be careful in the url you have given. That url where the callback value is present should not have any other elements except jsonp callback function. ie., first element of that page (even it should not have any html tags at first), should be jsonp callback function what you have given.

Upvotes: 1

user1850534
user1850534

Reputation:

By the same you tried and just adding $.each using jQuery for getting each fieldData from the response.

 <script>
      function gContent(){
        alert('working');

        $.getJSON('http://www.celeritas-solutions.com/pah_brd_v1/productivo/getGroups.php?organizationCode=att&userId1&', function(data) {
           $.each(data, function(i, fieldData){

            $("p").append(fieldData + " ");  //appending fieldDatas to paragraph tag

          });
        });
      }
    </script>

Upvotes: 1

trrrrrrm
trrrrrrm

Reputation: 11812

You can't call another domain using getJSON you have to call the same domain. you can solve this by one of the following:

  1. if you have access to the 2nd domain you can add Access-Control-Allow-Origin header
  2. if you have access to the 2nd domain You can change the request to JSONP and add a callback
  3. if you don't have access to the 2nd domain you have to use a backend language for example in PHP, you create a new page for example calling_the_url.php and you do file_get_contents('url'); and in your javascript you call your calling_the_url.php instead.

Upvotes: 1

Ashis Kumar
Ashis Kumar

Reputation: 6544

You can use the JSON.stringify(), to print the data as string to the DOM.

$(".paragraph").html(JSON.stringify(data));

check this http://jsfiddle.net/cc8HX/

Upvotes: 0

Raj
Raj

Reputation: 21

You can try this:

jQuery.parseJSON('{"name":"John"}');

Upvotes: 0

Related Questions