javascript - XMLHttpRequest url 包含 (&)
问题描述
所以我有这段代码可以将数据发送到后端
var $word = "minds & brains";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(xhttp.responseText);
}
xhttp.open("GET", 'http://mywebsite.com/controller/sample_controller?keyword='+$word, true);
xhttp.send();
但在我的 php 上,它只会引起人们的注意
<?php
echo $_GET['keyword'];
任何想法,帮助如何获得“头脑和大脑”这个词?
解决方案
您需要使用encodeURIComponent来转义&
字符:
xhttp.open("GET", 'http://mywebsite.com/controller/sample_controller?keyword='+ encodeURIComponent($word), true);
推荐阅读
- unit-testing - SpringBootTest - 请求无效时测试异常
- purescript - PureScript 中的全局资源(适用于 AWS Lambda)
- sql-server - SSRS 2017 带有日期的级联参数
- swift - 从匹配的关联值中获取枚举
- php - 使用 ajax (Codeigniter) 将数组从视图提交到控制器
- visual-studio-code - 用于访问当前活动调试会话的调试上下文、变量、异常的 VS Code 扩展
- c - 如何使用 LLVMBuildStore API
- html - 如何动态使所选颜色变暗
- wordpress - 检查数据库中是否已存在 post_content
- python - 小部件未显示在 Tkinter GUI 中