简体   繁体   English

将Javascript变量传递到JSON请求中

[英]Passing a Javascript Variable into a JSON request

Learner Driver Alert! 学习者驾驶员警报!

I'm trying to transfer the search field content from the form below named variable "name" & input it into the flickr API JSON Request tags field (line 40 below). 我正在尝试从命名变量“ name”下面的表单中传输搜索字段内容,并将其输入到flickr API JSON Request标签字段(下面的第40行)中。 I've tried all sorts of ways of declaring the variable & can't seem to find what I'm looking for on the web. 我尝试了各种声明变量的方法,但似乎找不到我在网上寻找的内容。 I'm guessing that its me not knowing exactly what I'm searching for. 我猜想那是我不完全知道我在寻找什么。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.getJSON demo</title>
  <style>
  img {
    height: 100px;
    float: left;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<form id="search">

  <p><input id="search-name" type="text" placeholder="Type Region Here"></p>
  <p><input id="search-submit" type="submit" value="Search For Region"></p>

</form>

<div id="images"></div>

<script>

  var name;

  $("#search").submit(function(event){
    event.preventDefault();
    var name = $("#search-name").val();
    console.log("Search Term Was: "+name);
    return false;
  });

   $("#search").submit(function(event) {
    (function() {
      var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
      $.getJSON( flickerAPI, {
        tags: name,
        tagmode: "any",
        format: "json"
      })
        .done(function( data ) {
          $.each( data.items, function( i, item ) {
            $( "<img>" ).attr( "src", item.media.m ).appendTo( "#images" );
            if ( i === 0 ) {
              return false;
            }
          });
        });
    })();
   });
</script>

</body>
</html>

Would anyone be so kind to help / put me in the right direction? 有人会这么乐于帮助我/朝正确的方向发展吗?

You do not need 2 event listeners for this. 您不需要2个事件侦听器。 var name = $("#search-name").val(); will create local scope for name hence you won't find value in global name . 将为name创建本地范围,因此您将无法在全局name找到值。

Try it this way: 尝试这种方式:

 $("#search").submit(function(event) { event.preventDefault(); var name = $("#search-name").val(); var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?"; $.getJSON(flickerAPI, { tags: name, tagmode: "any", format: "json" }) .done(function(data) { $.each(data.items, function(i, item) { $("<img>").attr("src", item.media.m).appendTo("#images"); if (i === 0) { return false; } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery.getJSON demo</title> <style> img { height: 100px; float: left; } </style> </head> <body> <form id="search"> <p> <input id="search-name" type="text" placeholder="Type Region Here"> </p> <p> <input id="search-submit" type="submit" value="Search For Region"> </p> </form> <div id="images"></div> </body> </html> 

Running the jQuery as so seems to work for me. 这样运行jQuery似乎对我有用。 You could test it out but also note that i changed the reference to flicker to be loaded as secure script: 您可以对其进行测试,但也请注意,我将对闪烁的引用更改为作为安全脚本加载:

var name;

$("#search").submit(function(event){
    event.preventDefault();
    name = $("#search-name").val();
    alert("Search Term Was: "+name);

    var flickerAPI = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
    $.getJSON( flickerAPI, {
        tags: name,
        tagmode: "any",
        format: "json"
    })
    .done(function( data ) {
        alert('done');
        $.each( data.items, function( i, item ) {
            $( "<img>" ).attr( "src", item.media.m ).appendTo( "#images" );
            if ( i === 0 ) {
                return false;
            }
        });
    })
    .fail(function() {alert('fail')});
});

JS Fiddle: https://jsfiddle.net/vsw3r31k/ JS小提琴: https : //jsfiddle.net/vsw3r31k/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM