Ajax调用不返回部分视图

时间:2015-03-25 12:53:43

标签: javascript c# jquery ajax asp.net-mvc

我正在进行AJAX调用,然后从Controller返回PartialView以在div中填充它。

以下是我的代码: -

var file = document.getElementById("file").files[0];

jq.ajax({
                type: "POST",
                url: '/Main/getData',
                data: formData,
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function (response) {  
                    jq('#partialPlaceHolder').html(response);    
                },
                error: function (error) {
                    alert("Some Error Occured");
                }
            });

 [HttpPost]
        public ActionResult getData(FormCollection form)
        {
            ......

            return PartialView("_pageMain", retMod);
        }

当我在Controller中调试代码时,直到最后都没有错误,但是AJAX仍会抛出alert("Some Error Occured")

PartialView未填充div。怎么解决这个问题?

2 个答案:

答案 0 :(得分:0)

您正在将返回数据类型设置为json

dataType: 'json'

尝试将此更改为

dataType: 'html'

请参阅此处http://api.jquery.com/jquery.ajax/

dataType (默认:智能猜测(xml,json,脚本或html)) 类型:字符串 您期望从服务器返回的数据类型。

答案 1 :(得分:0)

为什么不使用.load() JQuery .load()

  

描述:从服务器加载数据并将返回的HTML放入匹配的元素中。

简单:

$('#partialPlaceHolder').load("@Url.Action('getData')", { form: formData })

.load()的第三个参数是回调函数,所以随意使用它。

BTW:我没有在你的代码中找到使用变量file