gpt4 book ai didi

Javascript 数据逐个字母发送而不是搜索中的完整单词

转载 作者:行者123 更新时间:2023-11-29 11:00:08 25 4
gpt4 key购买 nike

尝试使用 ajax 和 jquery 访问 yahoo weather api。如果使用提交按钮进行搜索和提交,效果很好,但我希望仅使用回车键进行搜索。它一次只输入一个字母,而不是完整的搜索词。

  function makeAjaxCall(url, methodType,callback){ 
var xhr = new XMLHttpRequest();
xhr.open(methodType, url, true);
xhr.send();
xhr.onreadystatechange = function(){
if (xhr.readyState === 4){
if (xhr.status === 200){
console.log("xhr done successfully");
var resp = xhr.responseText;
var respJson = JSON.parse(resp);
callback(respJson);
} else {
console.log("xhr failed");
}
} else {
console.log("xhr processing going on");
}
}
console.log("request sent succesfully");

}



function processUserDetailsResponse(userData){ //Callback function
console.log(userData.query.results.channel.astronomy);




}
$('#inpt_search').keypress(function(e){
if(e === 'Enter'){
var city = $("#sunrise").value;
console.log(city);
e.preventDefault();
}
var url = 'https://query.yahooapis.com/v1/public/yql?q=select%20astronomy%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22'+ city +'%2C%20%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
makeAjaxCall(url, "GET", processUserDetailsResponse); enter code here //calling api using ajax
});

最佳答案

据我了解,您需要在按下 Enter 后进行 ajax 调用和更新。试试下面的代码,它只在按下 enter 时调用 API。

$('#inpt_search').keypress(function(e){
if(e.which === 13){
var city = $("#sunrise").value;
e.preventDefault();
var url = 'https://query.yahooapis.com/v1/public/yql?q=select%20astronomy%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22'+ city +'%2C%20%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
makeAjaxCall(url, "GET", processUserDetailsResponse);
}
});

关于Javascript 数据逐个字母发送而不是搜索中的完整单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48608875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com