简体   繁体   English

获取来自C#代码背后对javascript的响应

[英]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. 我正在进行jquery ajax调用以将数据发布到服务器上,并从是否成功完成返回响应。 But I am not able to see response in js. 但是我无法在js中看到响应。

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 在这里,我没有收到任何警报,但是能够在Chrome -> Inspect Element -> Network -> Response看到Chrome -> Inspect Element -> Network -> Response

cs 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. 将reponseType用作“ json”,然后尝试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);
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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