jquery.ajax()成功関数で特定の変数を参照する方法が不思議です。json_encodeをjquery.ajax()で使用して特定の値を返すにはどうすればよいですか?
addit.phpに私のPHPコードは
if ($_POST) {
$user_id = $_SESSION['user_id'];
$filename = $_SESSION['filename'];
$quote = $_POST['quote'];
$query = "INSERT INTO `submissions` (
`id` ,
`user_id`,
`quote` ,
`filename` ,
`date_added` ,
`uploaded_ip`
)
VALUES (
NULL , '{$user_id}', '{$quote}', '{$filename}', NOW(), '{$_SERVER['REMOTE_ADDR']}')
";
$db = DbConnector::getInstance();
$db->insert($query);
// remove funky characters from the quote
$cleanRemove = preg_replace("/[^a-zA-Z0-9\s]/", "", $quote);
// replace any whitespace with hyphens
$cleanQuote = str_ireplace(" ", "-", $cleanRemove);
// get the id of the last row inserted for the url
$lastInsertId = mysql_insert_id();
$returnUrl = array ("lastId"=>$lastInsertId, "cleanQuote"=>$cleanQuote);
echo json_encode($returnUrl);
}
私のjQueryのコード:警告で
$.ajax({
type: "POST",
url: "addit.php",
data: ({
// this variable is declared above this function and passes in fine
quote: quote
}),
success: function(msg){
alert(msg);
}
});
戻り値:
{"lastId":91,"cleanQuote":"quote-test-for-stack-overflow"}
どのように私は今、成功していることを参照することができます関数? (警告で「未定義」を返す)私はこのような何かをしようとしていたが、それはうまくいきませんでした:
$.ajax({
type: "POST",
url: "addit.php",
data: ({
// this variable is declared above this function and passes in fine
quote: quote
}),
success: function(data){
alert(data.lastId,data.cleanQuote);
}
});
恐ろしい!それは働いた。ありがとう..アラートはデバッグのためだけだった。私は実際に両方の変数を使用してユーザーをリダイレクトするためのURLを構築しており、完全に機能しています。再度、感謝します。 –
@bob_cobb偉大な、喜んで助けになる:) –