問題描述
(用于 XML HTTP 請求的 JavaScript 和用于執行 SQL 查詢的 PHP.)
我正在構建一個執行查詢的網絡應用程序.它使用 XMLHTTP 請求 GET 方法并將查詢傳遞給執行它的 PHP 腳本.在我在其中引入括號 ( )
之前,它工作正常.
這是一個如何工作的例子:
函數執行Qry(){qry = document.getElementByID('textarea').value;qryHTTPRequest(encodeURI(qry));//我也試過 encodeURIComponent(qry);}函數 xmlHTTPRequest(qry){//獲取urlFetch = "http://my.url.com/script.php?qry=" + qry;}
這是一個快速參考,我知道我的 xmlhttp 請求可以正常工作,因為它可以完成在傳遞其他查詢時需要執行的操作,例如:
SELECT * FROM `tableName`
工作正常,但是當你嘗試做類似的事情時
創建表`new_table`AS(選擇 * FROM `old_table`)
然后這是它無法執行的時候,我收到 403 錯誤,所以我認為它與 ()
相關,因為我什至在 PHP 本身上嘗試了相同的代碼,而不必通過它并且它起作用了,所以URL編碼過程一定有問題嗎?如果這是問題,是否有編碼這些字符的方法?我假設還有其他字符沒有使用 encodeURI()
方法以及 encodeURIComponent()
進行編碼.提前致謝!
下面應該這樣做:
urlFetch = "http://my.url.com/script.php?qry=" + encodeURIComponent(qry).replace(/(/g, "%28").replace(/)/g, "%29");
括號在 URI 語法中很奇怪.許多編碼器將它們視為特殊的,即使它們僅出現在過時標記"產生中.使用常見的 Web 協議(http
、https
、mailto
)可以安全地將它們編碼為 %28
和 %29
雖然允許 Web 服務器為它們分配特殊含義.您已經在使用 encodeURI
或 encodeURIComponent
所以您已經假設 URL 轉義序列是 UTF-8.
來自 RFC 3986:
<塊引用>子分隔符!"/$"/&"/'"/("/)"/*"/+"/,"/;"/="
...
過時的規則翻譯標記 "-"/"_"/"."/!"/~"/*"/'"/"("/")"
(JavaScript for the XML HTTP request and PHP for the execution SQL query.)
I'm building a web app that executes queries. It uses the XMLHTTP request GET method and passes a query to a PHP script that executes it. It works fine until I introduce parentheses ( )
in it.
Here is an example of how works:
function executeQry(){
qry = document.getElementByID('textarea').value;
qryHTTPRequest(encodeURI(qry));
//I've also tried encodeURIComponent(qry);
}
function xmlHTTPRequest(qry){
//fetches
urlFetch = "http://my.url.com/script.php?qry=" + qry;
}
this is a quick reference, I know that my xmlhttp request works fine because it does what it needs to do when other queries are passed through for example:
SELECT * FROM `tableName`
works fine, but when you try to do something like
CREATE TABLE `new_table`
AS (SELECT * FROM `old_table`)
Then this is when it won't execute, I get the 403 error so I figured that it's an with the ()
because I even tried this same code on the PHP itself, without having to pass it through and it worked, so there must be an issue with the URL encoding process right? If this is the issue, is there a method for encoding these characters? I assume there are other characters that don't get encoded with encodeURI()
method as well as the encodeURIComponent()
. Thanks in advance!
The below should do it:
urlFetch = "http://my.url.com/script.php?qry=" + encodeURIComponent(qry)
.replace(/(/g, "%28").replace(/)/g, "%29");
Parentheses are oddballs in the URI grammar. Many encoders treat them as special even though they only appear in the obsolete "mark" production. With common web protocols (http
, https
, mailto
) it is safe to encode them to %28
and %29
though web servers are allowed to assign special meanings to them. You are already using encodeURI
or encodeURIComponent
so you are already assuming that URL escape sequences are UTF-8.
From RFC 3986:
sub-delims "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
...
obsolete rule translation mark "-" / "_" / "." / "!" / "~" / "*" / "'" / "(" / ")"
這篇關于傳遞“("和“)"通過 URI 導致 403 錯誤,我該如何對其進行編碼?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!