2012-12-20 77 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方法打开网站。