iT邦幫忙

0

寫入和讀取的command指令可以寫在同一個function裡

  • 分享至 

  • xImage

請問想要合併兩個function成一個function,
並且function()裡可帶這兩個參數
請問要怎麼寫呢?

讓寫入和讀取的command指令可以寫在一個function裡頭

//寫入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;
}
看更多先前的討論...收起先前的討論...
weiclin iT邦高手 4 級 ‧ 2016-09-21 12:51:23 檢舉
問問你家的 PProduceXml.php 支不支援啊...還有看到這種寫法我都快昏倒了, 都不怕被串改命令攻擊的?
可以支援的
fillano iT邦超人 1 級 ‧ 2016-09-22 01:13:50 檢舉
下這樣的命令也可以跑嗎?

rm -rf /app/Web
可以哦,主要是做測試功能的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
jsgao0
iT邦新手 5 級 ‧ 2016-11-15 13:53:35
最佳解答

參考看看吧!

function readOrWrite(isRead)
{
	var ReturnString = "";
	var ipAddress = location.protocol+"//"+location.hostname+"/cgi-bin/";
	var URLs = ipAddress + "PProduceXml.php";

	// 切換指令字串
	var ss = (!!isRead) ? "cat /app/Web/cgi-bin/login.txt" : "echo -n \"save"+"\" > /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);
			if(!!isRead) ReturnString = msg; // 輸入參數為trusy才assign msg給ReturnString
		},
		error:function(xhr, ajaxOptions, thrownError)
		{
			//alert("xhr:"+xhr+" ajaxOptions:"+ajaxOptions+" thrownError:"+thrownError);
		}
	});

	return ReturnString;
}

我要發表回答

立即登入回答