[英]Javascript How to add string to url
I wonder how to add string at the end of the url in Js. 我想知道如何在Js的网址末尾添加字符串。 The url must to be available in a variable, so I can request it. 网址必须在变量中可用,因此我可以请求它。 And I get the inputs from a form. 我从表单获得输入。
example: 例:
http://localhost:3000/items/ -> http://localhost:3000/items/55ef09351ee9ce3c272ef8e7 http:// localhost:3000 / items / -> http:// localhost:3000 / items / 55ef09351ee9ce3c272ef8e7
Here's my code: 这是我的代码:
var link = "http://localhost:3000" + input._id.value
link = link.replace('?_id=', '');
var xhr = new XMLHttpRequest();
xhr.open("DELETE", link, false);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
With this code I get localhost:3000/items/?_id=55ef09351ee9ce3c272ef8e7 How can I make it without the ?_id= ? 有了这段代码,我得到localhost:3000 / items /?_ id = 55ef09351ee9ce3c272ef8e7如何在没有?_id =的情况下实现?
code from program 程序中的代码
<form onsubmit="submit" action="http://localhost:3000/items/" method="delete" name="del_stuff">
_id:<br>
<input type="text" name="_id">
<br>
<br>
<input type="submit" value="Submit">
</form>
</form>
<div id="output"></div>
<script type="text/javascript">
var form = function submit(e) {
// stop the regular form submission
e.preventDefault();
var link = "http://localhost:3000/items" + encodeURIComponent(input._id.value)
link = link.replace('?_id=', '');
// collect the form data while iterating over the inputs
var data = {};
for (var i = 0, ii = form.length; i < ii; ++i) {
var input = form[i];
if (input.name) {
data[input.name] = input.value;
}
}
// construct an HTTP request
alert("http request");
var xhr = new XMLHttpRequest();
xhr.open("DELETE", link, false);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
// send the collected data as JSON
xhr.send(JSON.stringify(data));
xhr.onloadend = function () {
// done
};
};
</script>
Thx 谢谢
使用encodeURIComponent
函数: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
var link = "http://localhost:3000" + encodeURIComponent(input._id.value)
I wonder how to add string at the end of the url in Js. 我想知道如何在Js的网址末尾添加字符串。 The url must to be available in a variable, so I can request it. 网址必须在变量中可用,因此我可以请求它。
location.href += input._id.value;
Now how you want to format the url is upto you. 现在,您要如何设置URL的格式由您决定。
With this code I get localhost:3000/items/?_id=55ef09351ee9ce3c272ef8e7 How can I make it without the ?_id= ? 有了这段代码,我得到localhost:3000 / items /?_ id = 55ef09351ee9ce3c272ef8e7如何在没有?_id =的情况下实现?
Please check out this fiddle , it worked fine! 请检查这个小提琴 ,它工作正常!
var link = "http://localhost:3000/?_id=55ef09351ee9ce3c272ef8e7"
link = link.replace('?_id=', '');
alert(link);
Normally you have to modify the .htaccess file. 通常,您必须修改.htaccess文件。 This is how you would define a rule to remove question mark from a URL, 这是您定义规则以从网址中删除问号的方式,
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(.*)$ $1%1? [R,L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.