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

javascript - jQuery $.getJSON() not working - Stack Overflow

programmeradmin1浏览0评论

I'm trying to parse a JSON response, but any instruction in the $.getJSON cannot be executed.

The json.html

<!DOCTYPE html>
<html>
    <head>
         <script src="js/jquery.js"></script>
    </head>
    <body>
         <script>
            $.getJSON("json.php?jsoncallback=?", function(data) {
               var a = data[0].cve_id + 'something';
            });
            alert(a); //chrome says "a is not defined"
         </script>
     </body>
</html>

The json.php:

<?php
header("Content-Type: application/json", true);
echo file_get_contents(".php?numrows=10&vendor_id=0&product_id=0&version_id=0&hasexp=1&opec=1&opov=1&opcsrf=1&opfileinc=1&opgpriv=1&opsqli=1&opxss=1&opdirt=1&opmemc=1&ophttprs=1&opbyp=1&opginf=1&opdos=1&orderby=1&cvssscoremin=0");
?>

Any idea why?

LATER:

Thanks for the replies. I've understood the reason.

I've switched from $.getJSON to $.get, specifying the data type to json.

I'm trying to parse a JSON response, but any instruction in the $.getJSON cannot be executed.

The json.html

<!DOCTYPE html>
<html>
    <head>
         <script src="js/jquery.js"></script>
    </head>
    <body>
         <script>
            $.getJSON("json.php?jsoncallback=?", function(data) {
               var a = data[0].cve_id + 'something';
            });
            alert(a); //chrome says "a is not defined"
         </script>
     </body>
</html>

The json.php:

<?php
header("Content-Type: application/json", true);
echo file_get_contents("http://www.cvedetails./json-feed.php?numrows=10&vendor_id=0&product_id=0&version_id=0&hasexp=1&opec=1&opov=1&opcsrf=1&opfileinc=1&opgpriv=1&opsqli=1&opxss=1&opdirt=1&opmemc=1&ophttprs=1&opbyp=1&opginf=1&opdos=1&orderby=1&cvssscoremin=0");
?>

Any idea why?

LATER:

Thanks for the replies. I've understood the reason.

I've switched from $.getJSON to $.get, specifying the data type to json.

Share Improve this question edited Nov 23, 2012 at 1:30 Schutzstaffel asked Nov 23, 2012 at 1:15 SchutzstaffelSchutzstaffel 2173 gold badges6 silver badges11 bronze badges 6
  • 1 Your AJAX is asynchronous. Variables have scope. – I Hate Lazy Commented Nov 23, 2012 at 1:17
  • 1 At the time alert(a) happens, the asynchronous AJAX call has not yet pleted. You need to alert(a) inside the function(data) callback. If you need to use the value to modify your DOM, you must do so in the callback – Michael Berkowski Commented Nov 23, 2012 at 1:18
  • 2 chrome says "a is not defined" - That's because a is defined as a local variable inside the callback function that you pass to $.getJSON(), so it can only be accessed inside that function (even aside from the async issue). – nnnnnn Commented Nov 23, 2012 at 1:20
  • I've also tried alert(a) inside function(data) and the alert doesen't pop up. Tried also console.log(a) and nothing shows up. – Schutzstaffel Commented Nov 23, 2012 at 1:23
  • If alert(a) inside the function doesn't work that suggests an error on the line before, e.g., if data isn't an array with a 0 element then data[0] is undefined and doesn't have a cve_id property. Please show the JSON structure that is retrieved. (Try console.log(data) as the first line of the function...) – nnnnnn Commented Nov 23, 2012 at 1:28
 |  Show 1 more ment

2 Answers 2

Reset to default 2

var a is defined inside the callback closure (anonymous function). One of the main reasons to use closures is that they produce private scope. I.e. any variables defined inside the function will not available to code outside of the function.

This will work:

$.getJSON("json.php?jsoncallback=?", function(data) {
    var a = data[0].cve_id + 'something';
    alert(a);
});

the JSON call is asynchronous so the callback function dont get dont get called until youre sever returns the data, But the javascript code keeps running to the next line of code. resulting in a stil being undefined try changing it to somthing along the lines of:

$.getJSON("json.php?jsoncallback=?", function(data) {
    var a = data[0].cve_id + 'something';
    alert(a)
});

you can define other methods outside of the callback and invoke em when the event happens whith the propper data

发布评论

评论列表(0)

  1. 暂无评论