JSON发送的数据始终为null

塞巴斯蒂安

第一个问题

我为另一个编码器使用代码。

我必须正确检查cron表达式,并且可以在程序中更改许多行代码。我从表单获取参数,然后尝试将此数据发送到控制器MVC

var outl = document.getElementById('Frequency').value;
var tmp = checkValid(outl);

function checkValid(checkExpression)
{
    var tmp = JSON.stringify(checkExpression);
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: String,
        type: 'POST',
        url: '/Service/isValid',
        data: tmp,
        success: function (isvalid)
        {
            return isvalid;
        }
    });
    console.log(tmp);
}

MVC中的控制器如下所示:

public JsonResult isValid(string dataFromJson)
{
    Options options = new Options()
    {
        ThrowExceptionOnParseError = true,
        Use24HourTimeFormat = true,
    };

    try
    {
        ExpressionDescriptor ceh = new ExpressionDescriptor(dataFromJson, options);
        string description = ceh.GetDescription(DescriptionTypeEnum.FULL);
        return Json(true);
    }
    catch (FormatException)
    {
         return Json(false);
    }
 }

参数dataFromJson均为null。为什么?真的,我看不到哪里出了问题。

第二部分

在控制器中,我返回JSON-这是个好主意吗?如何从Ajax中的此控制器获得响应

马杜

ajax如下更改数据。

function checkValid(checkExpression)
{
var tmp = JSON.stringify({dataFromJson : checkExpression});
$.ajax({
    contentType: 'application/json; charset=utf-8',
    dataType: "json",
    type: 'POST',
    url: '/Service/isValid',
    data: tmp,
    success: function (isvalid)
    {
        return isvalid;
    }
    });
   console.log(tmp);
}

在上面,我更改了var tmp = JSON.stringify({dataFromJson : checkExpression});此设置,现在可以将其绑定在服务器端。还要更改,dataType就像json您期望从服务器响应中获取json一样。

关于第二部分,它根据您的要求,您可以使用ContentJson根据您的需求为。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章