jQueryからPHPへのAJAX呼び出しがあります(PHPがjson_encode
配列で応答しますが、jQueryでは配列の値にアクセスできません)。
ステータスはOKですが、responseTextはundefined
です。jQueryのjson_encodeからデータを取得できません
$(document).ready(function() {
$("#comments_form").on("submit", function(e) {
e.preventDefault();
e.stopPropagation();
$.ajax({
type: 'POST',
url: 'process_in.php',
data: {
first: $("#firstname").val(),
second: $("#lastname").val(),
third: $("#mail").val(),
fourth: $("#phone").val(),
fifth: $("#message").val()
},
success: function(result) {
var x = jQuery.parseJSON(result);
alert(x.f);
},
});
});
})
<?php
include ('connection.php');
if (isset($_REQUEST['first']) && isset($_REQUEST['second']) && isset($_REQUEST['third']) && isset($_REQUEST['fourth']) && isset($_REQUEST['fifth']))
{
$firstname = $_REQUEST['first'];
$lastname = $_REQUEST['second'];
$email = $_REQUEST['third'];
$contact = $_REQUEST['fourth'];
$message = $_REQUEST['fifth'];
$data = array();
$data["f"] = xssafe($firstname);
$data["l"] = xssafe($lastname);
$data["e"] = xssafe($email);
$data["c"] = xssafe($contact);
$data["m"] = xssafe($message);
echo json_encode($data);
}
function xssafe($d)
{
$x = filter_var($d, FILTER_SANITIZE_STRING);
return $x;
}
コンソールにエラーがありますか?あなたのサーバーからの応答のHTTPステータスは何ですか?レスポンステキストは期待どおりのものですか?これをすべてデバッグしましたか?質問を編集して詳細を追加してください。 –
AJAXコール – Thamilan
に 'dataType: 'json''を追加し、' jQuery.parseJSON(result); 'を削除し、' result.f'を直接使用してください。 –