iT邦幫忙

0

請問如何讓Main()用到commandajax()
並在commandajax() 裡,可以使用savecmd和 catcmd兩個指令呢?

//初始載入畫面
function Main()
{
	
	//讀取floag資料
	alert("Main:");
	var savecmd = "echo -n \"save"+"\" > /app/Web/cgi-bin/login.txt";
	var catcmd = "cat /app/Web/cgi-bin/login.txt";
	
	var floag = funtion(command, isMsg);
	
function commandajax(command, isMsg)
{
    var ReturnString = "";
	var ipAddress = location.protocol+"//"+location.hostname+"/cgi-bin/";
	var URLs = ipAddress + "PProduceXml.php";
alert(2);
//    var savecmd = "echo -n \"save"+"\" > /app/Web/cgi-bin/login.txt";
//	var catcmd = "cat /app/Web/cgi-bin/login.txt";
//alert("Action=TransmissionStart&submitString="+(savecmd,catcmd));

	$.ajax(
		{
		url: URLs,
		data:"Action=TransmissionStart&submitString="+command,
		type:"GET",
		async: false,
		cache:false,
		success: function(msg)
		{
			alert("msg:"+msg);
			if(isMsg == true)
			{
				ReturnString = msg;
			}
		},
		error:function(xhr, ajaxOptions, thrownError)
		{
		}
	});
	
	return ReturnString;
	
}

1 個回答

0
fillano
iT邦超人 1 級 ‧ 2016-09-22 01:10:56
最佳解答

先跟你確定你的程式要在什麼環境下跑,瀏覽器端或是伺服器端。

在瀏覽器端跑shell script是不可能的。

在伺服器端,如果是在node.js環境,通常會直接用它的http client而不是jquery。

看更多先前的回應...收起先前的回應...
fillano iT邦超人 1 級 ‧ 2016-09-22 01:44:36 檢舉

原來是把命令上傳給伺服器跑喔...不要這樣做啦/images/emoticon/emoticon20.gif

主要是做測試功能的,所以不影響,會想知道做法^^

伺服器跑

weiclin iT邦高手 4 級 ‧ 2016-09-22 10:45:14 檢舉

這麼堅持...那你用 ; 把兩個命令分隔開試試看, 例如

cat xxxxx ; echo xxxxx > xxx
//-------------------------------------------
//寫入save字串到login.txt
function savewrite()
{

//write
   var ReturnString = "";
	var ipAddress = location.protocol+"//"+location.hostname+"/cgi-bin/";
	var URLs = ipAddress + "PProduceXml.php";

var ss = "echo -n \"save"+"\" > /app/Web/cgi-bin/login.txt";
//alert("write:"+ss);

//var URLs = ipAddress + "Command.cgi";
	$.ajax(
		{
		url: URLs,
		data:"Action=TransmissionStart&submitString="+ss,
		type:"GET",
		async: false,
		cache:false,
		success: function(msg)
		{
//alert("msg:"+msg);

		},
		error:function(xhr, ajaxOptions, thrownError)
		{
//alert("xhr:"+xhr+" ajaxOptions:"+ajaxOptions+" thrownError:"+thrownError);
		}
	});
	
	return ReturnString;
}



//讀取login.txt
function loginread()
{

        var ReturnString = "";
	var ipAddress = location.protocol+"//"+location.hostname+"/cgi-bin/";
	var URLs = ipAddress + "PProduceXml.php";

        var ss = "cat /app/Web/cgi-bin/login.txt";

$.ajax(
		{
		url: URLs,
		data:"Action=TransmissionStart&submitString="+ss,
		type:"GET",
		async: false,
		cache:false,
		success: function(msg)
		{
		
alert("msg:"+msg);
ReturnString = msg;
		},
		error:function(xhr, ajaxOptions, thrownError)
		{
//alert("xhr:"+xhr+" ajaxOptions:"+ajaxOptions+" thrownError:"+thrownError);
		}
	});

	return ReturnString;
}



//初始載入畫面
function Main()
{
	
	//讀取floag資料
	
	
	var floag = loginread();
	
//	var floag = loginread();
//alert("floag"+floag);

	//依照floag內容讀取不同區塊
	if(floag == "login")//讀取初始值
	{
		ReadGroup();
	}
	else if(floag == "save")//讀取修改值
	{
	
		ReadGroup1();
	}

}

其實原本是寫function savewrite() function loginread()兩個函數,如上圖
但要精簡化,寫成一個function

我要發表回答

立即登入回答