Get and javascript? like php's if $_GET[foo]

Get and javascript? like php's if $_GET[foo]

本文关键字:if GET foo javascript and like php Get      更新时间:2023-09-26

我正在编写一个javascript http web服务器节点,对于CDN项目,我已经习惯了PHP的

if ($_GET = "hello"); echo "Yey!"

但我不知道如何做到这一点,我是javascript的新手。

NodeJS

var url = require('url');
var url_parts = url.parse(request.url, true);
var query = url_parts.query;

没有NodeJS:
使用URI.js获取查询字符串。https://medialize.github.io/URI.js/

var uri = URI("urn:uuid:c5542ab6-3d96-403e-8e6b-b8bb52f48d9a?hello=world");
uri.protocol() == "urn";
uri.path() == "uuid:c5542ab6-3d96-403e-8e6b-b8bb52f48d9a";
uri.query() == "hello=world";

您可以尝试以下功能:

function httpGet(theUrl)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

然而,不鼓励同步请求,因此您可能需要使用以下内容:

function httpGetAsync(theUrl, callback)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.send(null);
}

注意:从Gecko 30.0(Firefox 30.0/Thunderbird 30.0/SeaMonkey 2.27)开始,由于对用户体验的负面影响,主线程上的同步请求已被弃用