首页 > 解决方案 > 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'];

任何想法,帮助如何获得“头脑和大脑”这个词?

标签: javascriptphp

解决方案


您需要使用encodeURIComponent来转义&字符:

xhttp.open("GET", 'http://mywebsite.com/controller/sample_controller?keyword='+ encodeURIComponent($word), true);

推荐阅读