Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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 Ajax vs window.location帮助我理解_Javascript_Html - Fatal编程技术网

Javascript Ajax vs window.location帮助我理解

Javascript Ajax vs window.location帮助我理解,javascript,html,Javascript,Html,最近,我在html中摆弄一个搜索栏,它接收输入,我把它作为我想要访问的URL中的最后一个参数添加。然后,我使用window.location.assign(newUrl)更改为这个新URL。这反过来会向我的服务器发出GET请求,而我的服务器正在向我正在使用的api发出GET请求。这有什么不好的原因吗?有人告诉我,这是一种黑客行为,我应该使用Ajax,有人能解释一下为什么这不是一种好方法吗 <input type="search" id="searchBar" placeholder="Se

最近,我在html中摆弄一个搜索栏,它接收输入,我把它作为我想要访问的URL中的最后一个参数添加。然后,我使用window.location.assign(newUrl)更改为这个新URL。这反过来会向我的服务器发出GET请求,而我的服务器正在向我正在使用的api发出GET请求。这有什么不好的原因吗?有人告诉我,这是一种黑客行为,我应该使用Ajax,有人能解释一下为什么这不是一种好方法吗

<input type="search" id="searchBar" placeholder="Search for your location">

<button onclick="handleSearchRequest()">Search</button>

<script>
    function handleSearchRequest() {
        var searchInput = document.getElementById("searchBar").value;
        window.location.assign("http://localhost:8080/weather/" + searchInput)
        alert("Successfully requested your weather information")
    }
</script>

搜寻
函数handleSearchRequest(){
var searchInput=document.getElementById(“searchBar”).value;
window.location.assign(“http://localhost:8080/weather/“+搜索输入)
警报(“已成功请求您的天气信息”)
}

如果使用窗口位置,则需要加载整个页面
=>您需要一次又一次地重新加载搜索栏
使用Ajax,您只需加载页面的必要部分
=>您只需要更新数据,而不需要更改页面的组件