Ajax处理MVC中的“无效的JSON原语”

时间:2017-10-17 07:05:15

标签: json

我在调用ajax时遇到了一些问题。

这是功能:

    function jsFunction(value) {
    var selectedCompany = document.getElementById("CompanyId");
    var selectedCompanyId = selectedCompany.options[selectedCompany.selectedIndex].value;

    $.ajax({
        type: "POST",
        data: { companyId: selectedCompanyId, supplierId: value },
        url:  "@Url.Action("CheckContract", "PaidWork")",
        dataType: 'json',
        traditional: true,
        contentType: 'application/json; charset=utf-8',
        processData: false,
        success: function (response) {
            if (response != null && response.success) {
                document.getElementById("errorMessage").style.display = 'hidden';
                alert(response.responseText);
            } else {
                // DoSomethingElse()
                document.getElementById("errorMessage").style.display = 'visible';
                alert(response.responseText);
            }
        },
        error: function (response) {
            alert("error!");  //
        }

    });
}

这是消息的div

 <div id="errorMessage" style="display:none"><strong class="alert-danger">@ViewBag.MessageContractNotExist</strong></div>

在我的view我有一条我想要显示的消息,取决于Json从控制器发送给我的响应。 这是控制器中的方法:

 public ActionResult CheckContract(int companyId, int supplierId)
 {
        bool result = true;
        if (!spService.ContractExists(companyId, supplierId, ContractType.SupplierSilviPrio))
        {
            result = false;
        }
        if(!result)
        {
           ViewBag.MessageContractNotExist = "Not exist!!!";

           return Json(new { success = false, responseText = "The attached file is not supported." }, JsonRequestBehavior.AllowGet);              

        }          
         return Json(new { success = true, responseText = "Your message successfuly sent!" }, JsonRequestBehavior.AllowGet);            
    }

问题是我不断收到此错误:无效的json原始对象 你能帮我解决一下我想念的问题吗?感谢

0 个答案:

没有答案