从c#代码获取回复到javascript

时间:2013-12-05 11:20:23

标签: c# javascript jquery

我正在进行jquery ajax调用以在服务器上发布数据并返回响应是否成功完成。但是我无法在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 });
    }
});

此处我没有收到任何提醒但能够在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);
    }
}

我失踪的地方?

1 个答案:

答案 0 :(得分:3)

将reponseType用作“json”并尝试response.d。还要添加错误功能以查找问题发生的确切位置

$.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);
   }
});