Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建一个简单的GoogleBooksAPI_Javascript_Html_Css_Api - Fatal编程技术网

Javascript 创建一个简单的GoogleBooksAPI

Javascript 创建一个简单的GoogleBooksAPI,javascript,html,css,api,Javascript,Html,Css,Api,我目前正在尝试使用javascript创建一个简单的GoogleBooksAPI。但是,当我使用控制台日志时,没有显示任何内容。我在这方面还是新手,所以任何帮助都将不胜感激 $(document).ready(function () { $("#myform").submit(function(){ var search = $("#books").val(); if(search == '') { alert("Please enter something!") } else {

我目前正在尝试使用javascript创建一个简单的GoogleBooksAPI。但是,当我使用控制台日志时,没有显示任何内容。我在这方面还是新手,所以任何帮助都将不胜感激

 $(document).ready(function () {

 $("#myform").submit(function(){

var search = $("#books").val();
if(search == '') {
  alert("Please enter something!")
}

else {
  var url = '';
  var img = '';
  var title = '';
  var author = '';

  $.get("https://www.googleapis.com/books/v1/volumes?q=" + search, function(response){

    console.log(response);

    //for(let i = 0; i < response.items.length; i++) {

      //title=$('<h5 class="center-align black-text">' + response.items[i].volumeInfo.title + '</h5>');
      //author=$('<h5 class="center-align black-text"> By:' + response.items[i].volumeInfo.authors + '</h5>');
      //img=$('<img class="aligning card z-depth-5" id="dynamic"><br><a href=' + response.items[i].volumeInfo.infoLink + '><button id="imagebutton" class="btn red aligning">Read More</button></a>');

      //url= response.items[i].volumeInfo.imageLinks.thumbnail;

      //img.attr('src',url);

      //title.appendTo("#result");
      //author.appendTo("#result");
      //img.appendTo("#result");

    //}

  });

}
 });

 return false;

});
$(文档).ready(函数(){
$(“#myform”).submit(函数(){
var search=$(“#books”).val();
如果(搜索=“”){
警报(“请输入内容!”)
}
否则{
var url='';
var img=“”;
var title='';
var author='';
$.get(”https://www.googleapis.com/books/v1/volumes?q=“+搜索、功能(响应){
控制台日志(响应);
//for(设i=0;i//img=$(“当我按下搜索按钮而不是接受输入时,我的应用程序正在刷新页面。我只是在此处添加了.preventDefault(),以阻止这种情况发生:

$("#form").submit(function(x) {
    x.preventDefault();
var search = $("#books").val();
if(search ==='') {
    alert("Please enter something!")
}

仔细检查您的输出,我只是复制粘贴您的
$.get()
和回调,然后从
控制台.log()获取结果,我很困惑!!发布您的JSFIDLE项目URL我确实找到了答案。我的应用程序在刷新页面时按下了搜索按钮。我刚刚添加了.preventDefault()一行代码阻止这种情况发生,现在它工作了!@SimonDehaut-这不是这个网站的工作方式…:)-解决了答案暗示了这一点,即使它不被接受。我们不会将这样的信息添加到标题中。您好,请添加一个稍微详细的答案版本。我知道您做了什么,但是,其他人会知道吗?我确信您不仅仅添加了
.preventDefault()
在您的代码中随机添加,它神奇地工作了。刚刚添加了一个更完整的解决方案。感谢您让我知道。