[英]Sending url with parameter to action function via javascript
I am trying to open new window using window.open(actionUrl) the actionUrl is compose form the action address and url as parameter. 我正在尝试使用window.open(actionUrl)打开新窗口,其中actionUrl由操作地址和url作为参数组成。 so eventually the actionUrl is : "/Default/Details?url= http://www.someaddress.com?a1=1&a2=2&a3=3 " However in the action the url i get is : " http://www.someaddress.com?a1=1 " I do not get "&a2=2&a3=3" parameters 所以最终actionUrl是:“ / Default / Details?url = http://www.someaddress.com?a1=1&a2=2&a3=3 ”但是,在操作中,我得到的URL是:“ http://www.someaddress .com?a1 = 1 “我没有获得”&a2 = 2&a3 = 3“参数
Here is the relevant view code: 这是相关的视图代码:
<div>
<input type="button" value="test" id="btnTest" />
</div>
<script>
var vurl = '@Url.Action("Details", "Default")';
$(function () {
$("#btnTest").click(function () {
var url = "http://www.someaddress.com?a1=1&a2=2&a3=3";
vurl = vurl + url;
window.open(vurl);
});
})
</script>
and this is the controller and action 这是控制者和行动
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
return View();
}
// GET: Default/Details/5
public ActionResult Details(string url)
{
return View();
}
}
You need to use the encodeURIComponent function on the url parameter's value: 您需要在url参数的值上使用encodeURIComponent函数:
var actionUrl = '/Default/Details?url=' + encodeURIComponent('http://www.someaddress.com?a1=1&a2=2&a3=3');
The &a2=2&a3=3
part was actually part of the /Default/Details
URL, not the http://www.someaddress.com
one. &a2=2&a3=3
部分实际上是/Default/Details
URL的一部分,而不是http://www.someaddress.com
。 Now that the inner URL is URI encoded, it should work. 现在,内部URL已使用URI编码,它应该可以工作了。
Make sure to decode the value when using the url
parameter though, using decodeURIComponent
: 确保使用时将值解码url
参数虽然使用decodeURIComponent
:
var urlMatch = location.search.match(/url=(.*)&?/);
if (urlMatch) {
var decodedUrl = decodeURIComponent(urlMatch[1]);
// do something with the decoded URL...
}
EDIT 编辑
For the first part (URI encoding) and based on your code, you should use it this way: 对于第一部分(URI编码),并根据您的代码,应按以下方式使用它:
<div>
<input type="button" value="test" id="btnTest" />
</div>
<script>
var vurl = '@Url.Action("Details", "Default")';
$(function () {
$("#btnTest").click(function () {
var url = "http://www.someaddress.com?a1=1&a2=2&a3=3";
vurl = vurl + encodeURIComponent(url);
window.open(vurl);
});
})
</script>
As for the ASP.NET part and the use of the string url
parameter, I'd suggest checking the following post: using decodeURIComponent within asp.net as I'm not familiar with this environment. 至于ASP.NET部分和string url
参数的使用,我建议检查以下内容:由于我对这种环境不熟悉,因此在asp.net中使用了解码URIComponent 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.