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

6
  • 1
    You have missed closing " in line url: "https://http://freetexthost.com/r56ct5aw03, Assuming it as typo Commented Feb 24, 2014 at 10:11
  • 1
    Uncaught SyntaxError: Unexpected token < this error usually when json parser tries to parse html Commented Feb 24, 2014 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? Commented Feb 24, 2014 at 10:17
  • so how can i upload my json file in web freely so that i can use that later on easily?? Commented Feb 24, 2014 at 10:20
  • Possible duplicate of jQuery AJAX cross domain Commented Dec 13, 2017 at 5:59

3 Answers 3

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

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

Comments

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);
    }
});

1 Comment

I'm not sure since he encountered a syntax error here.
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"
            }
        ]
    }
);

3 Comments

how can i upload my json file freely in web as jsonp and later on parse that easily??
You need to have a server side language like php to print the json text enclosed in the jsonp function name
You just need a normal hosting account

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.