繁体   English   中英

无法将json数据从控制器发送到ajax

[英]Trouble sending json data from controller to ajax

我有一个ajax / json / c#问题。 我想我正在做我在其他帖子中读到的所有内容,但仍然无法正常工作。 很可能是飞行员错误,但我错过了它。

任何想法都赞赏。

数据从电子邮件表单中获取并发送到控制器以发送电子邮件。 电子邮件被发送了! 这样可行。

我尝试将数据返回到ajax,但这似乎并没有起作用。

    [HttpPost]
    public JsonResult Sendemail(EmailStuff emailstuff)
    {
        string msg = sendmail(emailstuff); //this works fine - msg is returned as "ok"
        if (msg=="ok")
        {
            SendAutoReply();// this gets done
            return Json(new { success = true, message = msg }, JsonRequestBehavior.AllowGet);
        }
        return Json(new { success = false, message = msg }, JsonRequestBehavior.AllowGet); 
    }
//jquery
    $('#emailbtn').on('click', function (event) {
    //...get form data...
        var myData = {
            UserName: name, UserEmail: email, UserPhone: phone, UserAddress: address,
            UserSubject: subject, UserMessage: msg, UserList: emList
        };
    $.ajax({
            type: "POST",
            contentType: "application/json;charset=utf-8",
            processData: false,
            dataType: "json",
            url: $('#baseurl').text() + "email/sendemail",

            data: JSON.stringify(myData),
            success: function (response) {
                if (response.success === true) {
                    window.location = "email-ok";
                } else {
                    window.location = "email-error?msg="+response.message;
                }
            },
            error: function (response) {
                window.location = "email-error?msg=uhoh";
    })
})

在控制器中,字符串msg设置为“ok”。

我的期望是ajax成功:函数将被触发,然后if语句将评估response.success并重定向到email-ok或email-error页面。

但是会发生什么错误:函数被调用,重定向到电子邮件错误页面,消息“uhoh”

只有我知道要做的测试是Chrome DevTools>网络> XHR>预览,这显示:无法加载响应数据

所以看起来响应对象没有被正确发送。

提前致谢

编辑响应标题显示500错误 - 不确定是什么

    Response Header from Dev Tools
    cache-control: private
    cf-ray: 4daaae4d1eca9f46-IAD
    content-type: text/html; charset=utf-8
    date: Wed, 22 May 2019 00:30:20 GMT
    expect-ct: max-age=604800, report-uri="https://report- 
    uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    server: cloudflare
    status: 500
    x-aspnet-version: 4.0.30319
    x-aspnetmvc-version: 5.2
    x-powered-by: ASP.NET

编辑2

在下面显示的第二行中出现错误,错误显示“无法加载资源,服务器响应状态为500”

    //line 3954 of jquery-3.2.1.min.js.formatted 
    try {
            h.send(b.hasContent && b.data || null)
        } catch (i) {
          if (c)
             throw i
       }

似乎我有2个(愚蠢的)问题重叠。 除了控制器代码中显示的SendAutoReply之外,还有一个我没有在这里显示的保存操作。 我已经删除了两个以进行测试,但同时Intellisence建议我将return语句更改为元组

来自正确

    return Json(new { success = false, message = msg }, JsonRequestBehavior.AllowGet); 

错了

    return Json(( success = false, message = msg ), JsonRequestBehavior.AllowGet); 
~~~

So there were 2 problems. I corrected the Json statement but it took me a while to  
remove the Save statement - once I did that it worked.  Still not sure why there was a 500 error
Thanks for the suggestions

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM