C#中的异常不jQuery的阿贾克斯正确捕获异常、正确、jQuery、阿贾克斯

由网友(ゞ 这世界太冷清 °)分享简介:确定我要与生产环境中的问题,以及如何jQuery的AJAX捕捉错误。在我在我的本地机器的开发环境时,jQuery的AJAX调用Web服务[WebMethod的]和的WebMethod抛出一个特定的错误,jQuery的AJAX捕获正确的,但在生产envirnment jQuery的AJAX抓住了错误,但该消息是通用的有一...

确定我要与生产环境中的问题,以及如何jQuery的AJAX捕捉错误。

在我在我的本地机器的开发环境时,jQuery的AJAX调用Web服务[WebMethod的]和的WebMethod抛出一个特定的错误,jQuery的AJAX捕获正确的,但在生产envirnment jQuery的AJAX抓住了错误,但该消息是通用的有一个错误处理的要求。与内部服务器错误

例如

在C#code

  [WebMethod的]
公共字符串dostuff(字符串验证){
//一些code在这里
如果发生错误//
抛出新的异常(一些特定的错误信息);
返回确定;
}
 
2020中国异常流量报告 秒针系统 中欧商业评论

jQuery的AJAX调用

  VAR数据= JSON.stringify({验证:一些数据,这里的},NULL);

    $阿贾克斯({
        键入:POST,
        网址:/Services/memberService.asmx/dostuff,
        数据:数据,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        成功:函数(结果){
            警报(result.d); //提醒OK
        },
        错误:函数(XMLHtt prequest,textStatus,errorThrown){
            警报(responseText的=+ XMLHtt prequest.responseText +ñtextStatus =+ textStatus +ñerrorThrown =+ errorThrown);
        }
    });
 

在我的本地机器上的 XMLHtt prequest.responseText 是的一些具体错误消息

在生产环境中的 XMLHtt prequest.responseText 是时出错处理请求。

当地的环境是Windows 7家庭premium与IIS 7.5.7600.16385

生产环境为Windows Server 2008 R2与IIS 7.5.7600.16385(同开发环境)

为什么会有差别,如何使生产环境中抛出特定的错误?

的**的 * 的**的 * 的**的 * 的只是一个后续达.. TNX贾斯汀的的 * 的**的 * 的**的 * 的**的 * 的** * 的

我添加web.config文件中的服务文件夹中(DOH ...想知道为什么我没有想到这一点)

 < XML版本=1.0编码=UTF-8&GT?;
<结构>
    <的System.Web>
      <的customErrors模式=关/>
    < /system.web>
< /结构>
 

解决方案

在默认情况下,.NET应用程序配置为只显示具体的异常信息到本地服务器。

这是使攻击者无法获取你的网站根据他们的攻击特定的异常信息。它通常被认为是一个坏主意来改变这种行为。

如果您必须,但是,您可以更改:

 <的customErrors模式=仅限远程/>
 

要:

 <的customErrors模式=关/>
 

如果你想显示的服务文件夹中的错误:

 <位置路径=/服务>
    <的System.Web>
        <的customErrors模式=关/>
    < /system.web>
< /地点>
 

ok i have a problem with the production environment and how jquery ajax captures the error.

In my development environment on my local machine when the jquery ajax calls a webservice [webmethod] and the webmethod throws a specific error, the jquery ajax captures that correctly but in production envirnment the jquery ajax captures the error but the message is generic "There was an error processing the request." with "Internal Server Error"

for example

The c# code

[WebMethod]
public String dostuff(String Auth){
// some code here
// if error occurs
throw new Exception("Some specific error message");
return "ok";
}

the jquery ajax call

    var data = JSON.stringify({ Auth: "some data here" }, null);

    $.ajax({
        type: "POST",
        url: '/Services/memberService.asmx/dostuff',
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            alert(result.d); // alert ok
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("responseText=" + XMLHttpRequest.responseText + "n textStatus=" + textStatus + "n errorThrown=" + errorThrown);
        }
    });

on my local machine the XMLHttpRequest.responseText is "Some specific error message"

on the production environment the XMLHttpRequest.responseText is "There was an error processing the request."

the local environment is windows 7 home premium with IIS 7.5.7600.16385

the production environment is Windows Server 2008 R2 with IIS 7.5.7600.16385 (same as development environment)

why the difference and how to make the production environment throw the specific error?

********* just a follow up .. tnx Justin *************

i added web.config file inside the services folder (Doh... wonder why i didn't think about this)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
      <customErrors mode="Off" />
    </system.web>
</configuration>

解决方案

By default, .NET Applications are configured to only show specific Exception information to the local server.

This is so that attackers can't get specific Exception information about your site based on their attacks. It is typically considered a bad idea to change that behavior.

If you must, though, you can change:

<customErrors mode="RemoteOnly" />

To:

<customErrors mode="Off" />

And if you want to show the errors for the Services folder:

<location Path="/Services">
    <system.web>
        <customErrors mode="Off" />
    </system.web>
</location>

阅读全文

相关推荐

最新文章