0

I have what I hope is a fairly straightforward json question. I have a contact page where I use a jquery function to send data to a c# sendmail method. That all works. I am sending info back to the jquery but having trouble.

My c# is:

if (!ok) 
{
    return Json(new { success = false, responseText = "FAIL" }, 
     JsonRequestBehavior.AllowGet);
 }  
else
{
    return Json(new { success = true, responseText = "SENT" }, 
       JsonRequestBehavior.AllowGet);
}

and the ajax part of the jquery is:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
        processData: false,
        dataType: "json",
        cache:false,
        url: "x/sendEmail",
        dataType: 'json',
        data: JSON.stringify(myData),

        //complete changed to success
        success: function (response) {
            if (response != null && response.success) {
                alert(response.success + " pass" + response.responseText);
            } else {
                alert(response.success + " fail " + response.responseText);
            }
        },
        error: function (response) {
            alert(response.success + " fail2 " + response.responseText); 
        }

I get the response.success as true or false but response.responseText is always 'undefined'.

Not sure what I am missing

I changed the C# a little but same results

public class ResponseObject
{
    public bool success { get; set; }
    public string responseText { get; set; }
}

public ActionResult sendEmail(string userName, string userEmail, string 
  userPhone, string userAddress,string userSubject, string userMessage, string 
  userList)
 {
       ///code to send mail - works no problem

       ResponseObject response;

        if (!ok)
        {
            //  Send "false"
            response = new ResponseObject { success = false, responseText = "FAIL" };
        }
        else
        {
            //  Send "Success"
            response = new ResponseObject { success = true, responseText = "Your message successfuly sent!" };
        }

        return Json(response, JsonRequestBehavior.AllowGet);
}
2
  • Can you type in console.log(response) on success and see if that still contains undefined? Commented Jul 3, 2017 at 22:45
  • that doesn't give me anything if I try before clicking on the alert - gives me undefined after clicking on the alert. In my ajax though it is showing as not null before it shows the alert Commented Jul 4, 2017 at 15:12

2 Answers 2

3

Try returning Content instead (you will need Newtonsoft.Json package)

public ActionResult sendEmail(string userName, string userEmail,string userPhone, string userAddress, string userSubject, string userMessage, string userList)
{
       ///code to send mail - works no problem
        if (!ok)
        {
            //  Send "false"
            var response = new { success = false, responseText = "FAIL" };
            return Content(Newtonsoft.Json.JsonConvert.SerializeObject(response), "application/json");
        }
        else
        {
            //  Send "Success"
            var response = new { success = true, responseText = "Your message successfuly sent!" };
            return Content(Newtonsoft.Json.JsonConvert.SerializeObject(response), "application/json");
        }
}
Sign up to request clarification or add additional context in comments.

2 Comments

same thing success=true but responseText is undefined
What's the output of console.log(response) in the client side ??
0

Press F12 to open the dev. tools, go to the "Network" tab and make that AJAX call again. Now you should be able to to find the url calling the sendEmail function. Click on it, go to the "Response" tab and see the response being sent by the server. There you'll be able to verify if you're receiving both properties and the format being used (for example, maybe there's a typo in the property name).

5 Comments

Ok... On the road but will try that when I get home... Thank!
I click on the submit button and then get the alert which shows "true pass undefined". I am checking the response tab before clicking on the alert and it is blank. The response is not null because I test for that, just not sure where the responseText is. I also tried with explicitly defining the object as shown in the update. All the fields pass intellisence so they are spelled correctly...?
Actually you need to look the response after sending the AJAX request. The main idea is to see how is MVC returning the result to see if it has any difference compared with the one that you're expecting. Honestly, the real answer here will be to debug your javascript code by setting a breakpoint inside your success callback and then checking the value of your "response" variable, but since you're using alerts to see the contents, I'm guessing that you're still not familiar with the debugging tools.
No I'm not really familiar, I don't use this enough. I've used breakpoints before for jscript and jquery but had trouble using them with Ajax and json. I can give that a try tonight and see what happens. Thanks
Maybe your problem was that you weren't considering that AJAX requests are performed in an asynchronous way, so you couldn't get it to debug the callback function because it was skipping it. To avoid this, set the breakpoint inside your callback function, by doing this, you'll be able to debug that function, once the server has returned a response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.