简体   繁体   中英

Get response from c# code behind to javascript

I am making jquery ajax call to post data on server and return response from whether completed successfully or not. But I am not able to see response in js.

js:

$.ajax({
    url: 'ajaxExecute.aspx/CAO2',
    data: strRequest,
    dataType: "json",
    contentType: "application/json",
    cache: false,
    context: document.body,
    type: 'POST',
    success: function (response) {
        alert(response);
        window.parent.$('#divDialog').dialog('close');
        window.parent.$('#divDialog').dialog('destroy');
        window.parent.$('#divDialog').html(response);
        window.parent.$('#divDialog').attr('title', 'Error');
        window.parent.$('#divDialog').dialog({ show: "blind", modal: true, dialogClass: 'alert', zIndex: 99999, resizable: false, draggable: false });
    }
});

here i am not getting any alert but able to see response in Chrome -> Inspect Element -> Network -> Response

cs

[WebMethod]
public static void CAO2(string Guardian, string CIFID, string EmploymentType)
{
    try
    {
        if (Guardian != "" && CIFID != "" && EmploymentType != "" )
        {
            if (Generix.putCustomerDetailsPart2(Guardian,CIFID,EmploymentType)) // Will Create SQL Query And Execute on Database
            {
                HttpContext.Current.Response.Write("Information Submitted Successfuly..!!<br/><br/>Please Visit Your Nearest Branch...");
            }
            else
            {
                HttpContext.Current.Response.Write("Error Occurred While Processing Information..!!<br/><br/>Please Try Again...");
            }
        }
        else
        {
            HttpContext.Current.Response.Write("Missing Information..!!");
        }
    }
    catch (Exception xObj)
    {
        HttpContext.Current.Response.Write("ERROR: " + xObj.Message);
    }
}

where I am missing?

Use the reponseType as "json" and try response.d. Also add error function to find where exactly the problem occurs

$.ajax({
   url: 'ajaxExecute.aspx/CAO2',
   data: strRequest,
   dataType: "json",
   contentType: "application/json",
   responseType:"json",
   cache: false,
   context: document.body,
   type: 'POST',
   success: function (response) {
    alert(response.d);
   },
   error: function(xhr) {
    alert(xhr.responseText);
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM