html - How to call url in javascript? - Stack Overflow

I´m creating frontend for app and i need to call api function. I've got a url from api, which give

I´m creating frontend for app and i need to call api function. I've got a url from api, which gives me json data like this:

{ "id": 1, "name": "First Like", "description": "Like at least 1", "image": "images/badges/FIRST_LIKE" }

I need to call some of those items in my website.

<script type="text/javascript">
            //url to call  
        function getList(){

           var name = url.getItem("description");

       document.getElementById("name").innerHTML = name.toString();

            }

I know im doing this pletely wrong, and its my first atempt to do something like this, so I would really apreciate the help. Where should and how should I add url to code so i can pick a items i need? Thanks

EDIT: So i tried to replace with the ideas you gave me, and its still not working. Can anyone tell me what am I doing wrong?

    <script type = "texxt/javascript" src=".10.2.js"></script>
<script type="text/javascript" language="javascript">


<head>
 $.getJSON('', function (data) {
                var name = data;
                document.getElementById("name").innerHTML = name.toString();

            });

</script>
</head>
<body onload=getList()>
<div id="TopHeader">
    <div id="header" class="container">
        <div id="menu">
            <ul>
                <h1 id="name"></h1>
            </ul>
        </div>
    </div>
</div> 
</body></html>

Just trying to write a JSON string to page. Not working. I guess I´m doing something wrong.

I´m creating frontend for app and i need to call api function. I've got a url from api, which gives me json data like this:

{ "id": 1, "name": "First Like", "description": "Like at least 1", "image": "images/badges/FIRST_LIKE" }

I need to call some of those items in my website.

<script type="text/javascript">
            //url to call http://eclipsewildflyserver-gobanit./rest/something/id=1 
        function getList(){

           var name = url.getItem("description");

       document.getElementById("name").innerHTML = name.toString();

            }

I know im doing this pletely wrong, and its my first atempt to do something like this, so I would really apreciate the help. Where should and how should I add url to code so i can pick a items i need? Thanks

EDIT: So i tried to replace with the ideas you gave me, and its still not working. Can anyone tell me what am I doing wrong?

    <script type = "texxt/javascript" src="https://code.jquery./jquery-1.10.2.js"></script>
<script type="text/javascript" language="javascript">


<head>
 $.getJSON('http://eclipsewildflyserver-gobanit.rhcloud./AssignToolWebApp/rest/id=1', function (data) {
                var name = data;
                document.getElementById("name").innerHTML = name.toString();

            });

</script>
</head>
<body onload=getList()>
<div id="TopHeader">
    <div id="header" class="container">
        <div id="menu">
            <ul>
                <h1 id="name"></h1>
            </ul>
        </div>
    </div>
</div> 
</body></html>

Just trying to write a JSON string to page. Not working. I guess I´m doing something wrong.

Share Improve this question edited Mar 14, 2016 at 7:23 M.Redy asked Mar 12, 2016 at 18:55 M.RedyM.Redy 411 gold badge2 silver badges6 bronze badges 1
  • You're going to have to use an ajax call, maybe consider using jQuery to make this easier on yourself. – Anders Elmgren Commented Mar 12, 2016 at 18:58
Add a ment  | 

4 Answers 4

Reset to default 1

You have a url to call http://eclipsewildflyserver-gobanit./rest/something/id=1 and that is .json file . Therefore try this. You can use jQuery .getJSON() function:

$.getJSON('http://query.yahooapis./v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables/alltableswithkeys&callback', function(data) {
    //data is the JSON string
});

Just replace this url with your relevant url.

Checkout the json handling examples for the jquery library here on their docs page. There are some simple examples of using the getJson ajax method here as well.

In plain JavaScript, you should use the XMLHttpRequest object. It's explained here HTTP GET request in JavaScript?

You can also do it easily with jQuery this way:

$.get(yourURL, function(your_response){/*Do whathever you want with your_response*/})

There are a few problems with you current code. First, your have the head tag in your script tag. Second, you tell the browser to run the "getList()" function when the body loads, but they "getList()" function is not defined. I assume you mean this to be the your API call.

I am not sure what you plan to do with the data. I am sure you do not mean to convert a JSON to a string and then set the content of that entire JSON to an h1 element. You probably need to parse your desired content from the JSON that is returned from the $.getJSON call to your API. You can sort that out later.

Modify your code as follows to successfully call your API and load the resulting JSON into the h1 tag:

<head>
<script type = "texxt/javascript" src="https://code.jquery./jquery-1.10.2.js"></script>
<script type="text/javascript" language="javascript">

 window.onload = $.getJSON('http://eclipsewildflyserver-gobanit.rhcloud./AssignToolWebApp/rest/id=1', function (data) {
                document.getElementById("name").innerHTML = data.toString();
            });

</script>
</head>
<body>
<div id="TopHeader">
    <div id="header" class="container">
        <div id="menu">
            <ul>
                <h1 id="name"></h1>
            </ul>
        </div>
    </div>
</div> 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744943825a4602493.html

相关推荐

  • html - How to call url in javascript? - Stack Overflow

    I´m creating frontend for app and i need to call api function. I've got a url from api, which give

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信