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 객체를 기대하고있다. 폼을 제출하고 응답을 읽는 콜백 함수를 갖는 방법을 알려주는 간단한 예제를 찾으려고 고군분투하고 있습니다.
감사
편집 (지도 리처드 덕분에)
이 작업 버전입니다.
<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 이벤트를 두어 아마 두 번 제출 될 것입니다. 양식의 작업 (url)은 http : // myserver/myapp/statusupdates/submit입니다. 그것은 자바 애플 리케이션이며, 그것에 브레이크 포인트를 찔러 넣을 수 있다는 것을 알 수 있습니다. 그러나 이제는 제가 잘못했는지 궁금합니다. 양식을 제출할 때 위의 함수가 호출되었다고 생각했습니다. 양식을 제출 한 다음 JSON 응답을 읽는 좋은 예를 알고 있다고 생각하지 않습니까? 감사합니다 – Richard
그 예를 들어 주셔서 감사합니다, 지금은 이해가 시작됩니다. 아픈 시도, 환호 – Richard
마침내 작동! 당신이 옳았는데, 제 서버 설정은 옳지 않았습니다 : 저는 Spring MVC를 사용하고 있습니다 만, Jackson이 Maven 의존성을 갖지 않았습니다. 일단 내가 추가하면, 내 메소드는 실제로 JSON을 'load'함수로 리턴한다. 내가 게시 한 예제 (링크의 첫 번째 페이지)를 사용했는데 이제는 제대로 작동합니다. 의견이 아닌 내 질문에 대한 답변을 작성하면 신용으로 바꿀 수 있도록 올바른 것으로 표시하겠습니다. 고마움 백만,이 나를 천천히 정신 나간 운전되었습니다 :) – Richard