Codehelp
Codehelp

Reputation: 4747

Ajax call never returns

I have the following ajax call:

$.ajax({
            type: "POST",
            url: urlToGetRules,
            data: { ruleName: ruleName},
        })
             .success(function (data) {
                 document.location.href = "/StudentRules/GetAllStudentRules?productId=test";
             })
             .fail(function (xhr) {
                 alert("Something went wrong!!!")
                 console.log(xhr.responseText);
             });

In my controller I am creating a document in DocDb like so:

[HttpPost]
public ActionResult UpsertDoc(string ruleName)
{
            StudentRule studentRule = new StudentRule() { Id = Guid.NewGuid().ToString(), StudentId = "test", Name = ruleName, RuleType = "Allow all updates", StartDate = DateTime.UtcNow.ToString() };
            _updateScheduleRulesManager.UpsertScheduleRule(studentRule);

            return Json(new { success = true });
}

The idea is to return to a page where I list all the rules once the user creates a new rule in the Add Rule page.

The above code executes fine and I can see the expected document in Docdb but the status for this call in Developer Tools shows 'pending'!!! The code in the Success never executes.

Can someone please guide me here?

Thanks in advance.

Upvotes: 3

Views: 1560

Answers (2)

hutchonoid
hutchonoid

Reputation: 33305

There is no .success handler.

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

You need to use done:

  $.ajax({
        type: "POST",
        url: '@Url.Action("UpsertDoc")',
        data: { ruleName: 'test'},
        }).done(function (data) {
            document.location.href = "/StudentRules/GetAllStudentRules?productId=test";            
        }).fail(function (xhr) {
        alert("Something went wrong!!!")
        console.log(xhr.responseText);
        });

Also remove the spacing before the .done and .fail.

Screen shot

Screen shot

Upvotes: 2

Pranay Rana
Pranay Rana

Reputation: 176886

i think there is problem you are not passinb productid in request check this in ajax call

data: { ruleName: ruleName},

no productid in it where as in you method productid is parameter

public ActionResult UpsertDoc(string ruleName, string productId)

your data element should be and add datatype

data: { ruleName: 'name of rule', productId: 'productid' }, 
datatype : 'json'

Upvotes: 0

Related Questions