折腾:
【已解决】Bootstrap的css中如何用js去提交http的get请求调用服务器的REST API
期间,代码:
html
<code> <form> <div class="form-group input_request"> <input type="text" class="form-control" id="inputRequest" placeholder="请输入您要说的话"> </div> <div class="form-group"> <button id="submit" type="submit" class="btn btn-primary btn-lg col-sm-3">提交</button> <button class="btn btn-secondary btn-lg col-sm-3" type="button">清除</button> </div> </form> <div class="text-center bg-light box-shadow mx-auto" style="width: 96%; height: 160px; border-radius: 21px 21px 0 0; padding-top: 20px;"> <p id="output">as a book-collector, i have the story you just want to listen!</p> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script> <!-- <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script> --> <script src="js/bootstrap.js"></script> <script src="js/main.js"></script> </body> </code>
js
<code>$(document).ready(function(){ $("#submit").click(function(event){ event.preventDefault(); ajaxSubmitQa(); }); function ajaxSubmitQa() { console.log("ajaxSubmitQa"); var inputRequest = $("#inputRequest").val(); console.log("inputRequest=%s", inputRequest); var encodedInputRequest = encodeURIComponent(inputRequest) console.log("encodedInputRequest=%s", encodedInputRequest); var qaUrl = "http://47.96.131.109:32851/qa"; console.log("qaUrl=%s", qaUrl); var fullQaUrl = qaUrl + "?input=" + encodedInputRequest console.log("fullQaUrl=%s", fullQaUrl); $.ajax({ type : "GET", url : fullQaUrl, success: function(respJsonStr){ console.log("respJsonStr=%s", respJsonStr); $('#output').empty(); // // if(result.status == "Done"){ // $('#getResultDiv ul').empty(); // var custList = ""; // $.each(result.data, function(i, customer){ // var customer = "- Customer with Id = " + i + ", firstname = " + customer.firstname + ", lastName = " + customer.lastname + " "; // $('#getResultDiv .list-group').append(customer) // }); // console.log("Success: ", result); // }else{ // $("#getResultDiv").html("<strong>Error</strong>"); // console.log("Fail: ", result); // } }, error : function(err) { $("#output").html("<strong>Error</strong>"); console.log("GET: ", qaUrl, " ERROR: ", err); } }); } }); </code>
出错:
<code>main.js:22 Uncaught TypeError: $.ajax is not a function at ajaxSubmitQa (main.js:22) at HTMLButtonElement.<anonymous> (main.js:6) at HTMLButtonElement.dispatch (jquery-3.3.1.slim.min.js:2) at HTMLButtonElement.v.handle (jquery-3.3.1.slim.min.js:2) </code>
Uncaught TypeError: $.ajax is not a function
jquery – TypeError: $.ajax(…) is not a function? – Stack Overflow
“Neither of the answers here helped me. The problem was: I was using the slim build of jQuery, which had some things removed, ajax being one of them.
The solution: Just download the regular (compressed or not) version of jQuery here and include it in your project.”
我这里也是用到是:
jquery-3.3.1.slim.min.js
所以去换用标准的jquery
jQuery
-》
https://code.jquery.com/jquery-3.3.1.min.js
https://code.jquery.com/jquery-3.3.1.js
https://code.jquery.com/jquery-3.3.1.min.map
改为:
<code><!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="js/jquery-3.3.1.js"></script> <!-- <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> --> </code>
然后就可以了。
【总结】
此处已经确保了,html代码底部加载的js中,先加载jquery,再去加载其他,比如自己的main.js的。
但是main.js中却找不到$.ajax
原因是:
之前参考bootstrap的sample的代码中用到的是slim版本的jquery,其已经去除了ajax了。
解决办法:
去官网:
下载普通版本的jquery:
“Download the compressed, production jQuery 3.3.1”
或:
“Download the uncompressed, development jQuery 3.3.1”
换上,即可。
转载请注明:在路上 » 【已解决】js调用ajax去get请求服务器接口出错:Uncaught TypeError: $.ajax is not a function