2012-03-17 112 views
0

我試圖使用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對象。我努力尋找一個簡單的例子,告訴我如何提交表單,然後有一個讀取響應的回調函數。

感謝

編輯(感謝Richard爲指導)

這是工作版本。

<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> 
+0

啊,我剛剛發現了我正在做的一些愚蠢的事情。我在表單中的提交按鈕上放置了一個onClick事件,因此它可能會提交兩次。表單的動作(url)是http:// myserver/myapp/statusupdates/submit雖然。這是一個Java應用程序,我可以在其中插入一個斷點並看到它正在被調用,但我現在想知道我是否全部錯了。我認爲上述功能在表單提交時被調用。不要以爲你知道提交表單然後閱讀JSON響應的好例子?謝謝 – Richard 2012-03-17 12:06:07

+0

感謝那個例子,它現在開始有意義。我試試吧,歡呼吧 – Richard 2012-03-17 12:56:31

+1

終於有工作了!你是對的,我的服務器配置不是很正確:我使用Spring MVC,但是我沒有把傑克遜當成一個Maven依賴項。一旦我添加了,我的方法確實將JSON返回到「加載」函數。我使用了您發佈的示例(鏈接中的第一個示例),現在它工作正常。如果您爲我的問題(而不是評論)創建答案,我會將其標記爲正確,以便獲得信用。感謝一百萬,這一直驅使我慢慢瘋狂:) – Richard 2012-03-17 17:47:02

回答

1

上通常解析一個XMLHttpRequest回覆一個JavaScript語法錯誤表示來自服務器的無效數據。我最喜歡的監控XMLHttpRequest流量的工具是Firebug。它分析JSON,所以如果有什麼不對,你會立即知道。

確定服務器的JSON數據有效後,請查看以下example from the Dojo documentation。我認爲它是做你想做的。

+0

不錯的一個:)再次感謝。 – Richard 2012-03-18 21:49:18