CFML如何从ajax响应中获取状态代码

问题描述 投票:0回答:1

我几乎用CFML制作了一个Web应用程序,但是我对处理错误有疑问。例如,我的Web应用程序的一部分可以执行用户输入的SQL代码。但是,如果用户提供的SQL代码无效,则它将无法正确执行,并且会出现如下错误:https://devintranet.dep.gov/oogns/sharedComponents.cfc500

这有道理。如果它完全可以帮助回答我的问题,那么以下是“网络”标签上的信息:enter image description here

我想要的是能够在状态代码返回500时捕获并能够据此采取措施。例如,如果我发出的请求的状态代码最终为500,则可以使用JS警报来告诉用户出了点问题。

这是sharedComponents.cfc中的函数:

<cffunction name="LoadAttribute" access="remote" returnformat="plain" returntype="string" >
    <cftry>

    <cfquery name ="AttributeQuery" datasource="RBDMS_UPDATE">
        SELECT *
        FROM dbo.OOGNS_Queries
        WHERE UPPER(QueryName) = UPPER(<cfqueryparam cfsqltype="varchar" value="#form.default_ProfileName#" />)
    </cfquery>

    <cfcatch>
      <cfoutput>
        #cfcatch.Detail#<br />
        #cfcatch.Message#<br />
        #cfcatch.tagcontext[1].line#:#cfcatch.tagcontext[1].template#
      </cfoutput> 
    </cfcatch>
      </cftry>

      <cfreturn AttributeQuery["#default_ColName#"][1] /> 

      </cffunction>

这是调用它的ajax函数:

 $.ajax({
         type: "POST",
            url: "sharedComponents.cfc",
            data: { method: "LoadAttribute",
                    default_ProfileName: ProfileName,
                    default_Entity: Entity,
                    default_ColName: ColName,
                  },
            datatype: "json"
    }).done(function(returnresult) { 

在那之后实际上有很多代码,但是我相信它与我正在尝试做的事情无关。从字面上看,如果您可以告诉我如何从响应标头中获取状态代码,我认为我会很好的。

ajax header response http-status-codes cfml
1个回答
0
投票

您的请求中有两个单独的问题。

  1. 服务器端捕获错误

    可能是所请求的列在AttributeQuery结果中不可用。因此,您必须在访问它之前检查一下。

    一种方法是:

<cfreturn structKeyExists(AttributeQuery, default_ColName) ?
    AttributeQuery[default_ColName] : "" /> 
  1. 在客户端捕获错误

要捕获HTTP错误,您可以按use the error() function jQuery's ajax() function provides,如Miguel-F和AndreasRu在其注释中提到的。

© www.soinside.com 2019 - 2024. All rights reserved.