简体   繁体   English

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

[英]Trouble sending json data from controller to ajax

I have an ajax/json/c# issue. 我有一个ajax / json / c#问题。 I think I'm doing everything that I've read in other posts but still not working right. 我想我正在做我在其他帖子中读到的所有内容,但仍然无法正常工作。 Very likely pilot error but i am missing it. 很可能是飞行员错误,但我错过了它。

Any ideas appreciated. 任何想法都赞赏。

Data is grabbed from an email form and is sent to the controller to send an email. 数据从电子邮件表单中获取并发送到控制器以发送电子邮件。 The email gets sent! 电子邮件被发送了! That works. 这样可行。

I try to return data to ajax but that doesn't seem to be working. 我尝试将数据返回到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";
    })
})

In the controller the string msg is set to "ok". 在控制器中,字符串msg设置为“ok”。

My expectation is that the ajax success: function will be triggered and then the if statement will evaluate response.success and redirect to the email-ok or email-error page. 我的期望是ajax成功:函数将被触发,然后if语句将评估response.success并重定向到email-ok或email-error页面。

But what happens is the error: function is called and the redirect is to the email-error page with the message "uhoh" 但是会发生什么错误:函数被调用,重定向到电子邮件错误页面,消息“uhoh”

Only test I know to do is Chrome DevTools>Network>XHR>Preview and this shows: Failed to Load Response Data 只有我知道要做的测试是Chrome DevTools>网络> XHR>预览,这显示:无法加载响应数据

So it looks like the response object is not being sent correctly. 所以看起来响应对象没有被正确发送。

Thanks in advance 提前致谢

EDIT The response header is showing a 500 error - not sure what that's about 编辑响应标题显示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

EDIT 2 编辑2

There is an error showing up in 2nd line shown below, error says "Failed to load resource, the server responded with a status of 500" 在下面显示的第二行中出现错误,错误显示“无法加载资源,服务器响应状态为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
       }

Seems I had 2 (stupid) issues that overlapped. 似乎我有2个(愚蠢的)问题重叠。 In addition to the SendAutoReply shown in the controller code there was also a save operation that I didn't show here. 除了控制器代码中显示的SendAutoReply之外,还有一个我没有在这里显示的保存操作。 I had removed both to test but at the same time Intellisence suggested that I changed the return statement to a tuple 我已经删除了两个以进行测试,但同时Intellisence建议我将return语句更改为元组

FROM CORRECT 来自正确

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

to WRONG 错了

    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