猿问

PHP _GET 不随 URL 动态更新

我正在尝试使用 _GET 方法动态获取查询参数。


我的功能看起来像这样:


var baseURL = "http://example.org";

// clears the last query params

history.pushState("", document.title, baseURL);

// retrieves the value from the HTML code

var value = document.getElementById('id').value;


// URL with updated query params

var updatedURL = document.URL + "?value=" + value;

// pushing the new URL without refreshing the page.

history.pushState("", document.title, updatedURL);


// URL looks something like this "http://example.org?value=1"

但是当我尝试使用_GET['value']从 URL 检索值时,它只获取页面初始化的值并且不会动态更新,有没有什么方法可以在不刷新页面的情况下检索这个值?


德玛西亚99
浏览 131回答 1
1回答

森林海

history.pushState()只需更改浏览器 URL 的值。它实际上并不发出服务器请求并加载 _GET 变量。这就是您无法访问它们的原因。要在不刷新页面的情况下执行此操作,您需要进行 ajax 调用。$.ajax({  type: "GET",  url: "yourpage.php",  data: {value:value},  cache: false,  success: function(result){     // do something with the returned data  }});
随时随地看视频慕课网APP
我要回答