私のサーバーには、次のコードがあります。
response.getWriter().write(myJsonString);
response.setContentType("application/json;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
return null;
これは、通常のフォーム送信または AJAX jQuery 送信を行うと、JSON オブジェクトを文字列として返します (画面に表示されるように)。しかし、応答を処理するためにhttp://jquery.malsup.com/form/#jsonにある malsup の例からコードを追加すると、アラートが表示されませんか? それが応答設定なのか、それともjQueryの取得が間違っているのかさえわかりません。
$('form[name=uploadForm]').submit(function() {
//ajax form submission
$('form[name=uploadForm]').ajaxSubmit({
// dataType identifies the expected content type of the server response
dataType: 'json',
// success identifies the function to invoke when the server response
// has been received
success: processJson
});
//cancels normal form submission
return false;
});
function processJson(data) {
// 'data' is the json object returned from the server
alert(data.message);
}
私は、フォームプラグインの使用を根絶するオプションを受け入れます (つまり、jQuery.post()
呼び出しのみを使用します)。