Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.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 脚本未正确接收url_Javascript_Windows_Batch File_Url_Space - Fatal编程技术网

Javascript 脚本未正确接收url

Javascript 脚本未正确接收url,javascript,windows,batch-file,url,space,Javascript,Windows,Batch File,Url,Space,我使用的是一个批处理和java脚本的组合,我发现它使用一个批处理文件从一个web站点检索html,而我们处理的那个脚本并没有返回我在firefox中使用url时所显示的所需输出 我用于提取html的脚本是: @if (@This==@IsBatch) @then @echo off rem **** batch zone ********************************************************* setlocal enableextensions

我使用的是一个批处理和java脚本的组合,我发现它使用一个批处理文件从一个web站点检索html,而我们处理的那个脚本并没有返回我在firefox中使用url时所显示的所需输出

我用于提取html的脚本是:

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone     *********************************************************

setlocal enableextensions disabledelayedexpansion

rem Batch file will delegate all the work to the script engine 
if not "%~1"=="" (
    cscript //E:JScript "%~dpnx0" %1
)

rem End of batch area. Ensure batch ends execution before reaching
rem javascript zone
exit /b

@end
// **** Javascript zone     *****************************************************

// Instantiate the needed component to make url queries
var http = WScript.CreateObject('MSXML2.ServerXMLHTTP.6.0');

// Retrieve the url parameter
var url = WScript.Arguments.Item(0)

// Make the request

http.open("GET", url, false);
http.send();

// If we get a OK from server (status 200), echo data to console

if (http.status === 200) WScript.StdOut.Write(http.responseText);

// All done. Exit
WScript.Quit(0);
call callurl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"
我试图为脚本提供的url是“天方夜谭”]

或者选择“天方夜谭”]

问题似乎是空格/+的问题,因为我提供给它的其他URL都没有使用空格或空格+

我调用脚本以提取html的方式是:

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone     *********************************************************

setlocal enableextensions disabledelayedexpansion

rem Batch file will delegate all the work to the script engine 
if not "%~1"=="" (
    cscript //E:JScript "%~dpnx0" %1
)

rem End of batch area. Ensure batch ends execution before reaching
rem javascript zone
exit /b

@end
// **** Javascript zone     *****************************************************

// Instantiate the needed component to make url queries
var http = WScript.CreateObject('MSXML2.ServerXMLHTTP.6.0');

// Retrieve the url parameter
var url = WScript.Arguments.Item(0)

// Make the request

http.open("GET", url, false);
http.send();

// If we get a OK from server (status 200), echo data to console

if (http.status === 200) WScript.StdOut.Write(http.responseText);

// All done. Exit
WScript.Quit(0);
call callurl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"
编辑:找到脚本来自的原始线程


我所做的唯一更改是Msxml2.XMLHTTP.6.0被更改为Msxml2.ServerXMLHTTP.6.0,因为原始脚本由于我发现的安全性而无法加载站点。

只需将空格
或加号
+
替换为URL编码的空格
%20


e、 g.“天方夜谭%20Nights”]

这样称呼cscript:

cscript //E:JScript "%~dpnx0" "%~1"
我认为不需要对空格进行编码,而是使用双引号(带
%22
)虽然这可能需要解析整个命令行(
%*
),但您可以尝试以下方法

setlocal enableDelayedExpansion
set "link=%*"
set "link=!link:"=%%22!"
....
 cscript //E:JScript "%~dpnx0" "%link%"

您还可以尝试使用并将整个命令行传递给脚本。

在这种情况下,问题是windows脚本主机使用参数中包含的双引号

Npcmaka显示:对url中的引号进行编码。在我看来,这是正确的(双引号是不安全的字符,应该进行编码)

另一种解决方案是不将URL作为参数传递给脚本,而是将其存储在环境变量中,然后在javascript部分从变量中检索值

@如果(@This==@IsBatch)@那么
@回音
rem****批处理区*********************************************************
setlocal enableextensions disabledelayedexpansion
rem确保获得对当前批处理文件的正确引用
调用:getFullBatchReference\u f0
rem批处理文件将所有工作委托给脚本引擎
如果不是“%~1”==”“(
设置“URL=%~1”
cscript//nologo//E:JScript“%\u f0%”
)
rem确保批处理在到达javascript区域之前结束执行
退出/b%errorlevel%
:getFullBatchReference returnVar
设置“%~1=%~f0”
后藤:eof
@结束
//****Javascript区域*****************************************************
//实例化进行url查询所需的组件
var http=WScript.CreateObject('MSXML2.ServerXMLHTTP.6.0');
//从环境变量中检索url参数
var url=WScript.CreateObject('WScript.Shell')
.Environment(“过程”)
.项目(“URL”);
var-exitCode=0;
试一试{
//提出请求
http.open(“GET”,url,false);
http.send();
//若我们从服务器(状态200)得到了确认,那个么将数据回送到控制台
如果(http.status==200){
Write(http.responseText);
}否则{
exitCode=http.status;
};
}捕获(e){
//失败了
Write('ERROR:'+e.description);
exitCode=1;
};
//都做完了。出口
退出(exitCode);
现在,它可以被称为

geturl.cmd”http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=[“天方夜谭”]

脚本似乎无法正确解释%20,可能是因为它是从批处理文件馈送的?您需要使用另一个
%
来转义
%
,因此,请改用
%%20
。它会像这样返回网站,而不是像这样返回。我会编辑第一篇文章,展示我如何从批处理文件调用脚本,可能我正在调用它wrong@SomethingDark-链接中还有双引号。我不认为空间是问题所在。正如第一篇文章所示,脚本是javascript和批处理的混合体,它被保存为callurl.cmd,正如文章所说的名称。我猜他指的是在批处理文件中移动到JS部分。@geisterfurz007-是的。我知道这种技术。问题在于双引号和脚本思考有两个参数。更改导致以下输出C:\Users\reddeath68\Desktop\test>test.bat“方法”未被识别为内部或外部命令、可操作程序或批处理文件“操作”未被识别为内部或外部命令、可操作程序或批处理文件。该命令的语法不正确。C:\Users\reddeath68\Desktop\test\callurl.cmd(39,1)Microsoft JScript编译错误:语法错误您的方法是正确的(将URL中的引号更改为
%22
),但使用命名参数无法解决任何问题。问题的原因是windows脚本主机删除了参数中的任何双引号,因此内部javascript代码不会将它们包含在URL中,从而导致请求失败。谢谢您的帮助,但我仍然无法获得我想要的输出。html应该列出一组带有文本的行,例如@reddeath68,测试为
geturl.cmd"http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=[“Arabian+Nights”]“
我得到了指定的输出。你得到了什么?我得到的输出和它在添加到cmd行时值得注意吗?它将url添加为”“注意双重问题marks@reddeath68,出于某种原因(我不知道为什么)从上一条注释复制命令行时,会出现不可见字符。我已将其包含在答案中。您可以测试它吗?这样做是为了首先将隐藏字符传递给脚本?