Bijay Rai
Bijay Rai

Reputation: 959

get content from external url using javascript

I am using freetexthost.com to store my json code.. and now i ve to get those content from url using javascript,jquery,ajax... bt am being unable to get it.. am trying following code

<!DOCTYPE html>
<html>
<head>
<title>Useless</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.11.0.min.js'></script>
<script type="text/javascript">

$.ajax({
  type:     "GET",
  url:      "http://freetexthost.com/r56ct5aw03",
  dataType: "jsonp",
  success: function(data){
    console.log(data);
  }
});

</script>
</head>
<body>

<div class="content" >Hello</div>

</body>
</html>

getting an error as `Uncaught SyntaxError: Unexpected token <

is there any chances we can manipulate content of other page(url) using js...

Upvotes: 2

Views: 36917

Answers (3)

Saeed Ahmadian
Saeed Ahmadian

Reputation: 1119

  1. in your json file create function:

     //----for example
     parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});
    
  2. then call this function by JSONP

    var result = $.getScript("http://freetexthost.com/r56ct5aw03?callback=parseResponse");
    

I hope to help you.

reference : JSONP

Upvotes: 2

SajithNair
SajithNair

Reputation: 3867

The content of the page http://freetexthost.com/r56ct5aw03 is html, it should be jsonp to parse properly

The only difference between json and jsonp is that when calling jsonp you will also pass a callback parameter

e.g. url:"http://freetexthost.com/r56ct5aw03?callback=myFunction", 

Now the server side should print the json enclosed in this function name like below.

myFunction(
    {
        "sites":
        [
            {
                "siteName": "123",
                "domainName": "http://www.123.com",
                "description": "123"
            },
            {
                "siteName": "asd",
                "domainName": "http://www.asd.com",
                "description": "asd"
            },
            {
                "siteName": "zxc",
                "domainName": "http://www.zxc.com",
                "description": "zxc"
            }
        ]
    }
);

Upvotes: 0

Felix
Felix

Reputation: 38102

You need to close your url using ":

$.ajax({
    type:     "GET",
    url:      "https://http://freetexthost.com/r56ct5aw03", // <-- Here
    dataType: "jsonp",
    success: function(data){
        console.log(data);
    }
});

Upvotes: 0

Related Questions