clarence_odbody
clarence_odbody

Reputation: 185

C# return json object

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);
}

Upvotes: 0

Views: 11243

Answers (2)

Fourat
Fourat

Reputation: 2447

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");
        }
}

Upvotes: 3

Esteban Cervantes
Esteban Cervantes

Reputation: 313

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).

Upvotes: 0

Related Questions