最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax request to JSON not working? - Stack Overflow

programmeradmin5浏览0评论

I am trying to get some data from an API that provides JSON responses. I am brand new to all this. Can someone look at my code and tell me if there is a syntax reason it won't work? I want to hit the button and have an alert pop up containing the data sent back from the request. I think this is the most basic programming thing you can do, and I cannot seem to get it to work.

<head>

<script type="text/javascript">
$.ajax({
    type: 'GET',
    url: '=********&fields=name',
    async: false,
    jsonpCallback: 'jsonCallback',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(data)
    {
        alert(data)
    }
})
</script>

</head>
<body>
<button onClick="$.ajax()">Run Code</button>
</body>
</html>

I am trying to get some data from an API that provides JSON responses. I am brand new to all this. Can someone look at my code and tell me if there is a syntax reason it won't work? I want to hit the button and have an alert pop up containing the data sent back from the request. I think this is the most basic programming thing you can do, and I cannot seem to get it to work.

<head>

<script type="text/javascript">
$.ajax({
    type: 'GET',
    url: 'http://openapi.etsy./v2/teams/8787?api_key=********&fields=name',
    async: false,
    jsonpCallback: 'jsonCallback',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(data)
    {
        alert(data)
    }
})
</script>

</head>
<body>
<button onClick="$.ajax()">Run Code</button>
</body>
</html>
Share Improve this question edited Aug 10, 2012 at 19:34 Steve Robbins 13.8k12 gold badges80 silver badges127 bronze badges asked Aug 10, 2012 at 19:29 glaemartglaemart 1271 silver badge12 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

I rewrote your code:

<head>
<script type="text/javascript" src="http://code.jquery./jquery-1.8.0.min.js"></script>
<script type="text/javascript">

function doStuff() {
    $.ajax({
        type: 'GET',
        url: 'http://openapi.etsy./v2/teams/8787?api_key=********&fields=name',
        async: false,
        jsonpCallback: 'jsonCallback',
        contentType: "application/json",
        dataType: 'jsonp',
        success: function(data)
        {
            alert(data)
        }
    });
}
</script>

</head>
<body>
<button onClick="doStuff()">Run Code</button>
</body>
</html>

Add an event handler unobtrusively with jQuery to button using an id.

<head>


</head>
<body>
<button id="button">Run Code</button>

<script src="http://ajax.googleapis./ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$('#button').click(function(e){e.preventDefault();
$.ajax({
    type: 'GET',
    url: 'http://openapi.etsy./v2/teams/8787?api_key=********&fields=name',
    async: false,
    jsonpCallback: 'jsonCallback',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(data)
    {
        alert(data)
    }
})
});
</script>
</body>
</html>

Yes, there are several problems:

  1. You are using jQuery, but you are not loading it.
  2. You try to invoke the call when the page loads.
  3. The onclick event handler tries to invoke $.ajax() call incorrectly (it does not have any parameters).

This is probably all.

发布评论

评论列表(0)

  1. 暂无评论