2014-03-01 119 views
0

我正在开始我的sailsJS使用过程。使用SailsJS和MongoDB进行Ajax更新

我有一个关于如何通过jquery ajax请求更新我的模型与MongoDB的问题。 这里是我的代码:

jQuery("a#identity-edit").on("click", function() { 

    $.ajax({ 
     url: '/character/update', 
     method: 'POST', 
     data: { 
      id: $("a#identity-edit").attr("data-id"), 
      name: 'myNewName' 
     }, 
     success: function() { 
      window.location('/character/show/'+$("a#identity-edit").attr("data-id")); 
     }, 
     failure: function(msg) { 
      alert("Fail : " + msg); 
     }, 
     error: function(xhr, status, text) { 
      alert(text); 
      var response = jQuery.parseJSON(xhr, responseText); 
      alert(response.error); 
     } 
    }); 

不幸的是,这个码是“错误”的处理程序有一个空的文本擦肩而过......

你能不能给我解释一下什么是错的吗?

由于通过提前

西里尔

+0

什么是错误的是,你可能有一个服务器端错误,这不是服务器代码。你需要在你的问题中显示那部分。 –

+0

对不起,但我不明白你的答案 –

+1

这不是一个答案,它是一个评论。您的问题**仅**显示您的客户端代码,您POST到服务器上的端点。我们需要看看那里发生了什么。 –

回答

0

投递通过jQuery的API,你需要做一些更多的工作,让您的数据准备:

$.ajax({ 
    url: '/character/update', 
    method: 'POST', 

    // Tell the server we're sending JSON--not strictly necessary with Sails, 
    // but recommended. 
    contentType: 'application/json', 

    // Don't URLencode data 
    processData: false, 

    // Stringify the data--otherwise it will send "[object object]" 
    data: JSON.stringify({ 
     id: $("a#identity-edit").attr("data-id"), 
     name: 'myNewName' 
    }), 

    success: function() { 
     window.location('/character/show/'+$("a#identity-edit").attr("data-id")); 
    }, 
    failure: function(msg) { 
     alert("Fail : " + msg); 
    }, 
    error: function(xhr, status, text) { 
     alert(text); 
     var response = jQuery.parseJSON(xhr, responseText); 
     alert(response.error); 
    } 
}); 

而且,你确定你想要POST?如果您使用Sails蓝图,则更新使用PUT完成。