2017-04-26 85 views
0

我的批处理脚本通过使用JavaScript语法的CScript来执行HTTP请求,就像我的示例中那样。将cscript输出返回到批处理

使用this approach(也seen here)和一些help on escaping我尝试了以下内容: “ECHO处于关闭状态”

@if (@[email protected]) @then 
@echo off 
rem **** batch zone ********************************************************* 
    setlocal enableextensions EnableDelayedExpansion 

    set OWNPATH="%~dpnx0" 

    if not "%~11"=="" (
     FOR /F "usebackq tokens=*" %%r in (`cscript //E:JScript %OWNPATH%`) DO SET RESULT=%%r 
     ECHO %RESULT% 
    ) 

    exit /b 

@end 
// **** JScript zone ***************************************************** 
// Instantiate the needed component to make url queries 
var http = WScript.CreateObject('Msxml2.XMLHTTP.6.0'); 

// perform request 
var requestURL = "http://myserver/api"; 

// Make the request 
http.open("GET", requestUrl, false); 
http.send(); 

WScript.Echo(http.ResponseText); 

// All done. Exit 
WScript.Quit(0); 

不幸的是,我得到一个消息,而不是%RESULT%中的字符串。

该脚本在Windows 2008 R2服务器上运行。

+4

你需要[延迟扩展(http://stackoverflow.com/a/30284028/2152082) – Stephan

+0

谢谢,得到它了! '回声!结果!'起作用! – BNT

+0

[批处理中变量的行为与预期不符]的可能重复(http://stackoverflow.com/q/30282784/692942) – Lankymart

回答