2

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

CleverSkull
  • 479
  • 3
  • 10
Bijay Rai
  • 961
  • 1
  • 12
  • 32
  • 1
    You have missed closing `"` in line `url: "https://http://freetexthost.com/r56ct5aw03,` Assuming it as typo – Satpal Feb 24 '14 at 10:11
  • 1
    Uncaught SyntaxError: Unexpected token < this error usually when json parser tries to parse html – SajithNair Feb 24 '14 at 10:14
  • Use your browsers network inspector, jsonp is not being returned. If you use the real url in a browser do you see raw json or json in an html page? – Alex K. Feb 24 '14 at 10:17
  • so how can i upload my json file in web freely so that i can use that later on easily?? – Bijay Rai Feb 24 '14 at 10:20
  • Possible duplicate of [jQuery AJAX cross domain](https://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) – parth patel Dec 13 '17 at 05:59
  • You're asking the question "You can I upload my JSON file", which makes me think the file that you are starting with contains a JSON object, but your script shows you using `dataType: "jsonp"`. If your file contains JSON then you will want to use `dataType: "json"` instead. But as others have pointed out, that URL seems to return an HTML page, and not a javascript or json document. – Mike K Jul 20 '18 at 21:34

3 Answers3

2
  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

Saeed Ahmadian
  • 1,112
  • 1
  • 10
  • 21
0

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);
    }
});
Felix
  • 37,892
  • 8
  • 43
  • 55
0

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"
            }
        ]
    }
);
SajithNair
  • 3,867
  • 1
  • 17
  • 23