开发者

Access a HTTP GET Request via Javascript

开发者 https://www.devze.com 2023-03-31 09:17 出处:网络
Basically I have written an AJAX script using pure javascript and I have a script that basically takes in a HTTP GET REQUEST.

Basically I have written an AJAX script using pure javascript and I have a script that basically takes in a HTTP GET REQUEST.

e.g. script.php?id=2

I want to use this id within javascript so it can be sent via AJAX to return data from my database.

How can I access the id value. It should be possible as the value should be present w开发者_JAVA百科ithin the headers of the loaded page.

Thanks!


Have a look at the window.location object, the search property has the vales you're interested in.

This function will return an object which has all the variables as keys:

function queryVars() {
    var q = window.location.search.slice(1).split(/&/g);
    var ret = {};
    for (i=0;i<q.length;i++) {
        var item = q[i].split(/=/);
        ret[item[0]]=item[1];
    }
    return ret;
}

From your page you could use it like this:

var myquery = queryVars();
window.alert(myquery.id);


This might be just what you're looking for:

http://www.netlobo.com/url_query_string_javascript.html

0

精彩评论

暂无评论...
验证码 换一张
取 消