ajax post表单后 url会变为url?id=%d%5c

ajax post表单后 url会自动改变。如下:
提交前:localhost:3000/
提交后:localhost:3000/?user%5Bname%5D=%E5%A4%A7%E6%B4%92%E5%BA%97

解决方法1:

$('#form-signup').on('submit', function(ev) {
  ev.stopPropagation();
  ev.preventDefault();
  ...
});

解决方法2:
form表单的那个button按钮注意下type
submit
这样做虽然也可以阻止,但是不建议这么做,有碍于语义化和不能键盘enter操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: To get the value of the `id` parameter from the URL `https://www.baidu.com/userphoto.html?id=3192060242672264723=1` using JavaScript, you can use the `URLSearchParams` API as follows: ``` const urlParams = new URLSearchParams(window.location.search); const id = urlParams.get('id'); ``` This will extract the value of the `id` parameter from the URL, which in this case is `3192060242672264723`. You can then use any method to pass this value to the backend. This could involve making an AJAX request or submitting a form with the `id` value as a parameter. Here is an example of how you could use AJAX to send the `id` value to a backend endpoint: ``` const xhttp = new XMLHttpRequest(); xhttp.open("POST", "/backend", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhttp.onreadystatechange = function() { if (this.readyState === 4 && this.status === 200) { console.log(this.responseText); } }; xhttp.send("id=" + id); ``` This sends a POST request to the `/backend` endpoint with the `id` value as a parameter. Note that you would need to replace `/backend` with the actual URL of your backend endpoint. ### 回答2: 在JavaScript中,我们可以使用`location.search`来获取URL中问号后面的参数部分。对于给定的URL https://www.baidu.com/userphoto.html?id=3192060242672264723=1,我们可以使用以下代码来获取id的值: ```javascript var urlParam = location.search.substr(1); // 获取问号后面的参数部分,去除问号 var paramArr = urlParam.split('&'); // 将参数分割成数组 var idValue = ''; // 用于存储id的值 // 遍历参数数组,查找名为"id"的参数,并获取其值 for (var i = 0; i < paramArr.length; i++) { var pair = paramArr[i].split('='); // 将参数拆分成键值对 if (pair[0] === 'id') { // 如果参数名等于"id" idValue = pair[1]; // 获取该参数的值 break; // 找到后跳出循环 } } // 将获取到的id值传给后端,这里以发送Ajax请求为例 // 假设后端接口为https://www.example.com/api,接受id作为参数 var requestUrl = 'https://www.example.com/api?id=' + idValue; // 发送Ajax请求 // ... ``` 通过上述代码,我们可以提取URL中的id值,并将其传递给后端。具体的后端处理逻辑根据不同的业务需求而有所不同。 ### 回答3: 在JavaScript中,可以通过以下代码获取URL中的id的值,并将其传递给后端: ```javascript // 获取URL中的完整地址 var url = window.location.href; // 利用正则表达式获取id的值 var reg = /id=([^&]+)/; var id = reg.exec(url)[1]; // 将id的值传递给后端 // 你可以使用Ajax或者form表单提交等方式将id传递给后端。以下是使用Ajax的示例: var xhr = new XMLHttpRequest(); xhr.open("POST", "http://后端地址", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { // 请求成功的处理 } }; xhr.send("id=" + id); ``` 上述代码首先通过`window.location.href`获取当前页面的URL,然后使用正则表达式提取URL中的id的值。接下来,可以通过Ajaxid的值传递给后端。在这个示例中,使用POST方法将id作为参数发送给后端,你需要将"http://后端地址"替换为你的后端接口地址。 当然,上述仅是一个示例,根据具体情况你可能需要根据后端接口的要求进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值