jQuery表单提交带有成功和错误消息

我正在使用jQuery进行表单提交和validation,从服务器端我收到JSON格式的响应。

我在jQuery对话框中显示消息但无法显示来自服务器的消息….

我的方法:

 //  

我的标记:

 

Spog added successfully!

ADD SPOG

json string如果spog存在于数据库中,我会得到:

 {"messageId":"errorMessage","message":"spog found with Name 10000 Description nuts"} 

更新1:

  //  

标记:

 

Spog added successfully!

An error occurred while adding spog:

正如@Sam所说,你需要调整你的成功回调,你还需要稍微调整你的HTML。

  

然后JS改变……

 success: function(data) { if ( data.messageId && data.messageId === 'errorMessage' ) { // server responded with an error, show the error placeholder // fill in the error message, and spawn the dialog $("#dialog-message") .find('.success').hide().end() .find('.error').show() .find('.message').text( data.message ).end() .end() .dialog({ resizable:false, height:180, modal: true, buttons: { Ok: function() { $(this).dialog('close'); } } }); } else { // server liked it, show the success placeholder and spawn the dialog $("#dialog-message") .find('.error').hide().end() .find('.success').show().end() .dialog({ resizable:false, height:180, modal: true, buttons: { Ok: function() { $(this).dialog('close'); } } }); } }, 

添加以上“success”: datatype: "json",

然后将成功改为:

 success: function(data) { $("#dialog-message").append('

'+data.message+'

').dialog({ resizable:false, height:180, modal: true, buttons: { Ok: function() { $(this).dialog('close'); } } }); },

基本上你需要;
a)告诉你的代码你的服务器将返回JSON(因此它应该评估它)
b)使用该JSON执行某些操作 – 例如,拉出消息并将其附加到对话框中

请理解上面的代码只是一个建议,我还没有测试过!