2014-02-11 59 views
0

有没有人知道为什么这不允许表单提交?我正在尝试将Rotten Tomatoes API与用户搜索功能结合使用。JQuery表单提交API AJAX

形态 - PHP页面

<form name="myform" action="" method="GET"><h3>Search for a movie here:</h3><br> 
    <input type="text" id="inputbox" value="">&nbsp; 
    <input type="submit" name="submit" value="Go!"> 

JAVASCRIPT

$('form[name="myform"]').submit(function() { 
    $('#films table').empty(); //removes previous search results before adding the new ones. 

    var apikey = "frceg2d5djxezaedgm3qq94h"; 
    var baseUrl = "http://api.rottentomatoes.com/api/public/v1.0"; 
    var moviesSearchUrl = baseUrl + '/movies.json?apikey=' + apikey; 
    var query = form.inputbox.value; //uses the value from the input box as the query search 



     // sends the query 
     $.ajax({ 
     url: moviesSearchUrl + '&q=' + encodeURI(query), 
     dataType: "jsonp", 
     success: searchCallback // if successful, run searchCallback function 
     }); 


    // receives the results 

    function searchCallback(data) { 
    $('#films table').append('Found ' + data.total + ' results for ' + query); 
    var movies = data.movies; 
    $.each(movies, function(index, movie) { 
     $('#films table').append('<tr><td width="70" rowspan="2"><a href="' + movie.links.alternate + 
     '" title="Click here to view film information for ' + movie.title + '."><img class="ajaximage" src="' 
     + movie.posters.thumbnail + '" /></a></td><td class="ajaxfilmlisttitle"><h3><a href="' + movie.links.alternate + 
     '" title="Click here to view film information for ' + movie.title + '.">' + movie.title + '</a></h3>Release year: ' 
     + movie.year + '</td></tr><tr><td class="ajaxfilmlistinfo">Audience Score: ' + movie.ratings.audience_score + 
     '%<br>' + 'Cinema Release Date: ' + movie.release_dates.theater + 
     '<br>Runtime: ' + movie.runtime + ' minutes</td></tr>'); 
    }); 
    }; 
    }); 
+0

似乎就好像你在document.ready()包装器中没有这个javascript一样。表单本身之前的javascript输出? –

+0

它以前没有文档就绪标签,但只能使用onClick函数。 –

回答

0

我用这个代码测试,它的工作原理:

<form name="myform" action="" method="GET"> 
    <h3>Search for a movie here:</h3> 
    <input type="text" id="inputbox" value="" />&nbsp; 
    <input type="submit" name="submit" value="Go!" /> 
</form> 

<script> 

    $(function(){ 

     $('form[name="myform"]').on('submit', function(e) { 
      e.preventDefault(); 

      $('#films table').empty(); //removes previous search results before adding the new ones. 

      var apikey = "frceg2d5djxezaedgm3qq94h"; 
      var baseUrl = "http://api.rottentomatoes.com/api/public/v1.0"; 
      var moviesSearchUrl = baseUrl + '/movies.json?apikey=' + apikey; 
      var query = $('#inputbox').val(); //uses the value from the input box as the query search 

      // sends the query 
      $.ajax({ 
       url: moviesSearchUrl + '&q=' + encodeURI(query), 
       dataType: "jsonp", 
       success: searchCallback // if successful, run searchCallback function 
      }); 

      // receives the results 
      function searchCallback(data) { 
       $('#films table').append('Found ' + data.total + ' results for ' + query); 
       var movies = data.movies; 
       $.each(movies, function(index, movie) { 
        $('#films table').append('<tr><td width="70" rowspan="2"><a href="' + movie.links.alternate + 
        '" title="Click here to view film information for ' + movie.title + '."><img class="ajaximage" src="' 
        + movie.posters.thumbnail + '" /></a></td><td class="ajaxfilmlisttitle"><h3><a href="' + movie.links.alternate + 
        '" title="Click here to view film information for ' + movie.title + '.">' + movie.title + '</a></h3>Release year: ' 
        + movie.year + '</td></tr><tr><td class="ajaxfilmlistinfo">Audience Score: ' + movie.ratings.audience_score + 
        '%<br>' + 'Cinema Release Date: ' + movie.release_dates.theater + 
        '<br>Runtime: ' + movie.runtime + ' minutes</td></tr>'); 
       }); 
      }; 
     }); 
    }); 
</script> 

有几个问题:

  • form.inputbox.value没有工作,将其更改为$('#inputbox').val()
  • 添加e.preventDefault(),以防止从被提交
  • 有一个失踪});
  • 变化$('form[name="myform"]').submit(function() {$('form[name="myform"]').on('submit', function(e) {
+0

不幸的是,这不是解决方案。 URL改变以识别提交,但它实际上不检索API数据。 –

+0

也许尝试将数据类型从'jsonp'更改为'json'? – Pierre

+0

我只是使用你的代码做了一个测试,它的工作原理。如果您阻止提交表单,该怎么办? '$('form [name =“myform”]')。submit(function(e){e.preventDefault();' – Pierre