2012-12-20 75 views
0

這是我的代碼谷歌Chrome擴展POST VS GET

chrome.windows.create({'url': "http://example.com/upload/upload.php?pictureID="+ theResponse + "&userID=" + localStorage["id"]+"&username="+ localStorage["mainLogin"]}, function(tab) { 
    // open window 
}); 

這種構造,看起來像網址:

http://example.com/upload/upload.php?pictureID=123&userID=1&username=jack 

我會調用這個方法GET - 怎麼樣形成GETPOST

如何用POST數據而不是GET數據打開窗口?

回答

1

我認爲你必須編寫一個HTML頁面來創建一個包含你的POST數據和目標URL的表單並提交表單。 這裏有一個簡單的例子:

<html> 
<head> 
<script> 
document.addEventListener('DOMContentLoaded', function() 
{ 
    location.search.substr(1).split('&').forEach(function(item) 
    { 
     var input = document.createElement('input'); 
     input.type = 'hidden'; 
     input.name = item.substr(0, item.indexOf('=')); 
     input.value = item.substr(item.indexOf('=') + 1); 
     document.getElementById('postform').appendChild(input); 
    }); 
    document.getElementById('postform').submit(); 
}); 
</script> 
</head> 
<body> 
<form action="http://example.com/upload/upload.php" method="post" id="postform"> 
</form> 
</body> 
</html> 

說這是test.html的在擴展的根目錄。致電

chrome.windows.create({'url': "test.html?pictureID="+ theResponse + "&userID=" + localStorage["id"]+"&username="+ localStorage["mainLogin"]}, function(tab) { 
    // open window 
}); 

將使用POST方法打開網站。