JavaScript Ajax Post Words,其中包含PHP中的符号和解码


javascript ajax post words which contains & symbol and decode in php

我正在尝试发送包含"&"的 ajax 帖子。

search.php?region=Middle%20East%20&%20Africa&city=&language=

但它只在 php 端返回"中东"

请帮助我

如果你使用的是jquery:

$.ajax({
    url: 'search.php',
    type: 'POST',
    data: { region: 'Middle East & Africa', city: '', language: '' },
    success: function(result) {
        // ...        
    } 
});

如果没有,您可以使用 encodeURIComponent 函数手动对值进行 URL 编码:

var region = encodeURIComponent('Middle East & Africa');
// TODO: send the encoded value
要在 URL

中使用&字符,必须先对其进行 URL 编码。PHP 有一个叫做 urlencode 的函数来做到这一点,它可以提供帮助。要使上面的字符串工作,它应该看起来像:

search.php?region=Middle%20East%20%26%20Africa&city=&language=

&变得%26的地方.这是我为找到这个而编写的代码:

<?php
print urlencode('&');
?>