0

I am new to html and javascript.As far as i know the following code should give an alert when i press "Get JSON Data" button.But the page is not giving me any response.Any help is greatly appreciated.

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $.getJSON("http://127.0.0.1:5000/2", function(result){
                    if (result.length == 0){
                        alert("nothing") ;
                    }
                    if (result.length){
                        alert("success") ;
                    }    
                    // $("div").append(myObject);
                });
            });
        });
    </script>
</head>
<body>
    <button>Get JSON data</button>
    <div></div>
</body>
</html>

3
  • 2
    what does your developer console log says? Commented May 27, 2015 at 9:29
  • 1
    what is the result in new page when you write 127.0.0.1/2 address bar? If you have an installed IIS in your local, you must publish a default site. Your tried address want to reach your local IIS default web page with default variable to value of 2. Commented May 27, 2015 at 9:41
  • @Nomesh DeSilva I have no idea what this is but it's giving me this error "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at 127.0.0.1:5000/2. This can be fixed by moving the resource to the same domain or enabling CORS." Commented May 27, 2015 at 12:16

2 Answers 2

1

I suspected that should be the Cross-domain issue. That is why I asked for the console log. you have couple of choices:

  • config the cross-domain headers from your servlet/backend response. (ex: if you're using a Servlet:)

    response.setHeader('Access-Control-Allow-Origin','*');

  • use jsonp call back

    $.getJSON("http://example.com/something.json?callback=?", function(result){ //response data are now in the result variable alert(result); });

The "?" on the end of the URL tells jQuery that it is a JSONP request instead of JSON. jQuery registers and calls the callback function automatically.

  • use $.ajax with CORS enabled or with jsonp

 ex:
   $.ajax({
     url: surl,
     data: { 
       id: id  // data to be sent to server
     },
     dataType: "jsonp",
     jsonp: "callback",
     jsonpCallback: "jsonpcallback"
   });

 // Named callback function from the ajax call when event fired.
 function jsonpcallback(rtndata) {
   // Get the id from the returned JSON string and use it to reference the target jQuery object.
   var myid = "#" + rtndata.id;
   $(myid).feedback(rtndata.message, {
     duration: 4000,
     above: true
   });
 }

  • or else, download and configure "CORS.jar" in your server side which will allow the cross-domain requests. HOW ?

Hope you can get some idea. follow which suits for you ..

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

Comments

0

Replace the JSON call with

$.getJSON("http://127.0.0.1:5000/2", function(result){
        if (result.length == 0){
            alert("nothing") ;
        }
        if (result.length){
            alert("success") ;
        }    
        // $("div").append(myObject);
    }).fail(function( jqxhr, textStatus, error ) {
       var err = textStatus + ", " + error;
       console.log( "Request Failed: " + err )
    });

That way you can see what goes wrong. The javascript looks OK, I suspect it's a server issue.

You could also try getting back JSON from some random source, like http://1882.no/API/business/get/results.php?q=skole&page=0

3 Comments

Yes,It must be the error with server.But with the server you gave it's working.But what i don't understand is i am getting the desired result when i run "curl -i localhost:5000/2" which is { "data": [ "{\"lat\": 28.4594965, \"lng\": 77.0266383, \"_id\": {\"$oid\": \"55656cdfcb682e138eadf64e\"}, \"num\": 3, \"title\": \"#OnePlustwo launching on June 01 ??? i just heard .. is it ?? really ???\"}" ] }
OK, so it's a CORS issue then. You have to enable CORS on your server, but beware that this opens the server to anyone if you are open to the internet. What server software are you using? This website should be able to help: enable-cors.org
I have no personal experience with flask, but this blogpost seems to be of help: davidadamojr.com/handling-cors-requests-in-flask-restful-apis Please post your own answer if that is the solution, including code, in case the website goes down and someone finds this question later :)

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.