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 create a document in DocDb like this:

[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 the page where I list all the rules as soon as the user creates a new rule on the Add Rule page.

The above code runs fine and I see the expected document in Docdb, but the status of this call in Developer Tools shows "pending" !!! The code in Success is never executed.

Can anyone help me here?

Thanks in advance.

+3


source to share


2 answers


No handler .success

.

Depreciation notice: jqXHR.success (), jqXHR.error () and jqXHR.complete () callbacks have been deprecated as of jQuery 1.8. To prepare your code for possible removal, use jqXHR.done (), jqXHR.fail (), and jqXHR.always ().

You need to use:

  $.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 .done

and .fail

.

Screenshot

Screen shot

+2


source


I think the problem is that you are not passinb productid

in the request check this on the ajax call

data: { ruleName: ruleName},

      

no productid in it where as in your productid method the parameter



public ActionResult UpsertDoc(string ruleName, string productId)

      

your item should be and add data type

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

      

0


source







All Articles