0

I have been stuck on this all day, the getJSON just isn't working, I have tried loads of examples, but it still isn't working... Any help would be great. Thanks!

    console.log("hi");
    $.getJSON("https://pod.cscf.me/conversations/new.json", function(result)
    {
            console.log("workingggggg");
            $.each(result, function(i, person)
            {
                    console.log("itworks2");
            }); 
    }); 

It only outputs the first console log "hi".

Here is the code from the new.JSON file:

    <script>
      //<![CDATA[
        $(document).ready(function () {
          var data = $.parseJSON( "[{\"value\":\"32\",\"name\":\"R M\"},{\"value\":\"17\",\"name\":\"[email protected]\"},{\"value\":\"15\",\"name\":\"Henry Hoggard\"},{\"value\":\"26\",\"name\":\"[email protected]\"}]" ),
              autocompleteInput = $("#contact_autocomplete");

          autocompleteInput.autoSuggest(data, {
            selectedItemProp: "name",
            searchObjProps: "name",
            asHtmlID: "contact_ids",
            retrieveLimit: 10,
            minChars: 1,
            keyDelay: 0,
            startText: '',
            emptyText: 'No Results Found',
            preFill: [{name : "",
                       value : ""}]
            });
          autocompleteInput.focus();
        });
      //]]>
    </script>

I want to access the var data = $.parseJSON etc section (contains the names and IDs)

9
  • are you using any other javascript also on your page, it might be a cause of a javascript conflict Try: jQuery.getJSON Commented Apr 27, 2013 at 14:34
  • if you look on the network tab is there an issue with the connection? Is it finding the server or is the server rejecting the connection? Is your server returning a valid response? If not you will not get to the callback in getJSON. You can use .ajax and setup an error handler also. Commented Apr 27, 2013 at 14:35
  • also it looks like your link to JSON feed is not working in Chrome, because of an untrusted Certificate Commented Apr 27, 2013 at 14:36
  • Are you logged in that webservice before that call? When I open the link in the browser I see 2 possible reasons for why it isn't working: 1. There's a SSL certificate error and after I proceed 2. The response is {"error":"You need to sign in or sign up before continuing."}. Commented Apr 27, 2013 at 14:36
  • @Mohit, yes it is untrusted, but its my own server and I accepted it before logging in. Commented Apr 27, 2013 at 14:40

1 Answer 1

2

Opening https://pod.cscf.me/conversations/new.json gives me response 401 unauthorized.

That is after confirming I want to install the certificate. There is a json response saying that I need to log in but the 401 response status header causes the request to fail.

Another thing is that you can't send xhr requests from site A to site B or from sub.A.com to A.com or from A:80.com to A:102.com unless the site (pod.cscf.me) sends a cors header.

I'm using forcecors plugin for firefox so can make requests to other sites using that.

More about cross site scripting and same origin policy can be found on wikipedia, it explains how JSONP (is not the same as JSON) can be used and CORS.

After logging in with the test account you provided and running the code in the console I see what you mean, no error but the callback is never called. Opening https://pod.cscf.me/conversations/new.json I can see that the url does not return JSON, the response is:

<script>
  //<![CDATA[
    $(document).ready(function () {
      var data = $.parseJSON( "[]" )

This is not JSON so the callback is never called since converting the response to a javascript object fails silently.

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

5 Comments

we have another working JSON file which grabs the contacts from diaspora
I've updated my answer, looks like the url in your code does not return JSON
ohh, so if we cant get it as JSON can we use REGEX to grab the array?
You should either produce JSON or change the response header Content-Type application/json; to Content-Type text/javascript;. I think the first option would be easier but if you provide text/javascript in the response you might be able to get the array with a regexp but than you'll have to parse it manually.
you're welcome. to grab the result as text you have to change the response header content-type to text/javascript and then use the following script in your page $.get(url,null,function(result){console.log(result)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.