Dojoを使用してサーバーに投稿しようとしています。サーバーはJSON応答を返しています(私はそれをデバッグして妥当な値を返していることを知っています)が、復帰時にJavascriptコンソールに「構文エラー」が表示されています。何か案は?xhr.postがJSON応答を受け取りません
function submitStatusUpdate() {
dojo.xhr.post({
form:"statusUpdateForm",
handleAs: "json",
load: function(data){
alert('Saved with id ' + data.id);
},
error: function(err, ioArgs){
// again, ioArgs is useful, but not in simple cases
alert('An error occurred');
console.error(err); // display the error
}
});
}
私もこの
function submitStatusUpdate() {
var posted = dojo.xhr.post({
form:"statusUpdateForm",
load: function(data){
},
error: function(err, ioArgs){
// again, ioArgs is useful, but not in simple cases
console.error(err); // display the error
}
});
posted.then(function(response){
alert('returned ' + response);
});
}
のようにそれを試してみた。しかし、私の警告にプリントアウトします応答はちょうど私の全体のページのHTMLのようです。私はJSONオブジェクトを期待しています。私は、フォームを送信する方法を教え、レスポンスを読み込むコールバック関数を持つ簡単な例を見つけるのに苦労しています。
おかげ
EDIT(指導のためのリチャードのおかげで)
これは作業バージョンです。
<script language="Javascript">
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.CheckBox");
function sendForm(){
var form = dojo.byId("myform");
dojo.connect(form, "onsubmit", function(event){
// Stop the submit event since we want to control form submission.
dojo.stopEvent(event);
// The parameters to pass to xhrPost, the form, how to handle it, and the callbacks.
// Note that there isn't a url passed. xhrPost will extract the url to call from the form's
//'action' attribute. You could also leave off the action attribute and set the url of the xhrPost object
// either should work.
var xhrArgs = {
form: dojo.byId("myform"),
load: function(data){
// As long as the server is correctly returning JSON responses, the alert will
// print out 'Form posted. ' and then the properties and values of the JSON object returned
alert("Form posted." + data);
},
error: function(error){
// We'll 404 in the demo, but that's okay. We don't have a 'postIt' service on the
// docs server.
alert("error");
}
}
// Call the asynchronous xhrPost
alert("Form being sent...");
var deferred = dojo.xhrPost(xhrArgs);
});
}
dojo.ready(sendForm);
</script>
これは私のフォームがどのように見えるのか(種類)です。これはとにかく(私の本当の形ははるかに大きいです)動作します。興味深いことに、私はそれを動作させるために[...]ボタン]に[... =「提出」入力タイプ]タグを私の通常を変更しなければならなかった、適切
<form method="post" id="theform" action="postIt">
<input value="Some text" name="formInput" type="text"/>
<input name="checkboxInput" type="checkbox"/>
<button id="submitButton" type="submit">Send it!</button>
</form>
ああ、私はちょうど私がやっている愚かなものを見つけました。私はフォームの送信ボタンに上記の関数を呼び出すonClickイベントを置くので、おそらく2回提出されています。フォームのアクション(url)はhttp:// myserver/myapp/statusupdates/submitです。それはJavaアプリケーションであり、ブレークポイントをスティックして呼び出されているのを見ることができますが、私はそれがすべて間違っているのか疑問に思っています。私はフォームが提出されるときに上記の関数が呼び出されたと思った。フォームを送信してからJSONレスポンスを読むなど、良い例が分かっているとします。ありがとう – Richard
その例をありがとう、今感覚を作り始める。うん、それは試して、歓声を与える – Richard
それは最後に働いて!あなたは正しかった、私のサーバーの設定は間違っていた:私は春のMVCを使用していますが、私は依存関係としてジャクソンを持っていない。これを追加すると、私のメソッドはJSONを実際に 'load'関数に戻しました。あなたが投稿した例を使用しました(リンクの最初のページ)、今は正常に動作します。コメントではなく私の質問への回答を作成した場合は、それを正しいものとしてマークして、クレジットを取得します。ありがとう、百万、これは私をゆっくりと狂って運転しています:) – Richard