2011-04-19 37 views
1

我使用的是ASP.NET MCV3,jQuery 1.5.2和jQuery表单插件。
这里的示例代码:ASP.NET MVC3和jQuery表单插件

<script type="text/javascript"> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
     $('#uploadForm').ajaxForm({ 
      dataType: 'json', 
      beforeSubmit: function() { alert('beforeSubmit'); }, 
      success: function() { alert('success'); }, 
      error: function() { alert('error'); } 
     }); 
    }); 
</script> 

<form id="uploadForm" action="@Url.Action("UploadFile")" method="post"> 
    <input type="submit" value="Submit file" /> 
</form> 

[AcceptVerbs(HttpVerbs.Post)] 
public JsonResult UploadFile() 
{ 
    return Json(new { message = "success" }); 
} 

当我提交表单,我总是得到以下错误消息:预期“;”
我搜索了SO,谷歌..但找不到解决这个问题的任何方法。

我发现达林的评论here,但我需要有beforeSubmit成功事件。

任何帮助将不胜感激!

+0

问题解决了吗?如果是这样,我建议您将解决方案作为*答案*发布到您自己的问题中,并将其标记为解决方案。 (对于何时可以这样做有时间限制,所以您可能需要等待几天,然后才能在绿色复选标记中打勾......) – 2011-04-19 15:39:52

+0

@Tomas,是的,问题已解决。我必须等待几天才能回答我的问题。如果有人有更好的答案,我会很乐意接受他/她的回答。 – 2011-04-19 16:38:09

回答

0

我将dataType从json改为text,然后我解析了结果。一切接缝工作正常。

<script type="text/javascript"> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
     $('#uploadForm').ajaxForm({ 
      dataType: 'text', 
      beforeSubmit: function() { alert('beforeSubmit'); }, 
      success: processJson, 
      error: function() { alert('error'); } 
     }); 
    }); 

    function processJson(responseJson) { 
     var obj = jQuery.parseJSON(responseJson); 
     alert(obj.message); 
    } 
</script>