jQuery $.ajax のデフォルトの contentType はapplication/x-www-form-urlencodedです。これは、jQuery がコンテンツをエンコードすることを意味します。ただし、別の contentType を指定しているため、データはエンコードされないため、独自のエンコードを行う必要があります。( Jquery ajax エンコーディング データ)
encodeURIComponentを試してください。( AJAX リクエストに対して jQuery で文字列を URL エンコードする)
特定の文字の各インスタンスを、文字の UTF-8 エンコーディングを表す 1、2、3、または 4 つのエスケープ シーケンスで置き換えることにより、Uniform Resource Identifier (URI) コンポーネントをエンコードします (2 つの「サロゲート」で構成される文字の場合、4 つのエスケープ シーケンスのみになります)。 "文字)。
例:
エンコードされた var = encodeURIComponent(str);
完全な解決策:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css"/>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"/>
<script>
$(document).ready(function() {
$("#autocomplete").autocomplete({
source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"],
close: function (event, ui) {
$.ajax({
url: 'yourAjaxRequestHandleFile.php',
data: 'q=' + encodeURIComponent($('#autocomplete').val()),
type: 'get',
success: function (ajxResponse) {
alert(ajxResponse);
}
});
}
});
});
</script>
</head>
<body>
<label for="autocomplete">Select a programming language:</label>
<input id="autocomplete">
</body>
</html>
// ここに PHP コードがあります
<?php
// Get Send Ajax Data
$q = $_GET['q'];
// Show Ajax Data and return to Ajax
echo "You submitted $q ";
?>