我已尝试过此代码的每个变体,似乎没有任何工作。我想将一个搜索词提供给一个函数,并捕获API调用引发的任何错误。
错误如下:
GET insight.bitpay.com/block/afasfasf 404 (Not Found)
x.support.cors.e.crossDomain.send
@ ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js:6x.extend.ajax
@ ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js:6x.(anonymous function)
@ ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js:6searchFunction
@ (index):109(anonymous function)
@ VM11642:2InjectedScript._evaluateOn
@ VM11641:904InjectedScript._evaluateAndWrap
@ VM11641:837InjectedScript.evaluate
@ VM11641:693
代码:
function searchFunction(category, searchTerm) {
$.get("https://insight.bitpay.com/" + category + "/" + searchTerm, function(data) {
return data;
}).fail(function() {
return "fail";
});
}
// --------------- Search bar --------------- //
$(document).ready(function() {
$("#search-form").on("submit", function(e){
e.preventDefault();
var searchTerm = $("#srch-term").val();
var searchBlockHash = searchFunction("block", searchTerm);
if (searchBlockHash != "fail") {
// some function(searchBlockHash)
} else {
// some error message
}
});
});

<form id="search-form" class="navbar-form navbar-left">
<div class="input-group input-group-sm" style="min-width:512px">
<input type="text" class="form-control" placeholder="Search for a block, transaction or address" name="srch-term" id="srch-term">
</div>
</form>
&#13;