1

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>
18
  • possible duplicate of Ways to circumvent the same-origin policy Commented Sep 16, 2013 at 10:33
  • So what error are you getting Commented Sep 16, 2013 at 10:35
  • I guess, you need to modify the title a bit. Commented Sep 16, 2013 at 10:39
  • I am calling gContent() function onclick of heading tag, before JSON code, alert() is working but after JSON code there is no result appear in alert(), and want to append data into .paragraph div. Here is JSON result link i want to append into div. Commented Sep 16, 2013 at 10:45
  • are you calling the same site? Commented Sep 16, 2013 at 10:45

5 Answers 5

1

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1
 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.

Comments

0

You can try this:

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

Comments

0

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/

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.