0

The ajax post hits the server and delivers the correct information, but even though it works it still is hitting the error function. The ready state is 0, so it says it isn't even making the request.

            var serviceURL = '/ContactForm/FirstAjax';
            $.ajax({
                type: "POST",
                url: serviceURL,
                data: JSON.stringify(formInfo),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                            alert("Worked");
                        },

                error: function (xhRequest, ErrorText, thrownError) {
                    alert("Failed to process correctly, please try again" + "\n xhRequest: " + xhRequest + "\n" + "ErrorText: " + ErrorText + "\n" + "thrownError: " + thrownError);

                }
            });

The error message is:Error Msg

My controller looks like this:

    [HttpPost]
    [ActionName("FirstAjax")]
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public JsonResult FirstAjax(ContactForm contactForm)
    {            
        return Json("works", JsonRequestBehavior.AllowGet);
    }
mwhite14
  • 257
  • 1
  • 7
  • 19

2 Answers2

0

There's an error in your ajax post method or your controller. If you leave the ajax method as is, you can modify your controller to:

[HttpPost]
[ActionName("FirstAjax")]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public JsonResult FirstAjax()
{            
    var postedData = new StreamReader(Request.InputStream);
    var jsonEncoded = postedData.ReadToEnd(); //String with json
    //Decode your json and do work. 
    return Json("works", JsonRequestBehavior.AllowGet);
}

If you don't want to change the controller, then you need to change the javascript to:

var serviceURL = '/ContactForm/FirstAjax';
$.ajax({
        type: "POST",
        url: serviceURL,
        data: { contactForm: JSON.stringify(formInfo) },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {
                     alert("Worked");
                },
        error: function (xhRequest, ErrorText, thrownError) {
            alert("Failed to process correctly, please try again" + "\n xhRequest: " + xhRequest + "\n" + "ErrorText: " + ErrorText + "\n" + "thrownError: " + thrownError);

        }
    });

Hope this helps.

lopezbertoni
  • 3,551
  • 3
  • 37
  • 53
  • I tried both, still getting an error. With what I had it was passing the object correctly, and I was able to use it in the controller. But even with it working, the error would show. The ready state from it is 0 which means it didn't initiate the request, could it be firing too fast? – mwhite14 Jul 11 '13 at 20:06
  • It can be a cross-domain issue: Take a look at http://stackoverflow.com/questions/6818939/jquery-post-respond-with-readystate0-status0, http://stackoverflow.com/questions/3586780/jquery-ajax-post-unsuccessful – lopezbertoni Jul 11 '13 at 20:19
0

I was using Fiddler and realized it was doing a GET not a post POST. I figured it out how to fix it. When I submitted the form, it called the Javascript with onsubmit="submitForm();". By submitting the form it was preforming a GET on the url instead of a POST. I fixed with onsubmit="submitForm(); return false".

Answer Source - jQuery: why does $.post does a GET instead of a POST

Community
  • 1
  • 1
mwhite14
  • 257
  • 1
  • 7
  • 19