使用 jquery 显示 ajax 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15438907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:02:25  来源:igfitidea点击:

displaying an ajax response with jquery

jqueryajaxcoldfusioncoldfusion-9

提问by Chris Pilie

I have a Coldfusion cfc that queries a database for data. I would like to call that cfc and display the ajax response within a div. Eventually I would like to format the response with html. Currently I am having trouble with displaying the response. This is what I have so far.

我有一个 Coldfusion cfc,可以查询数据库中的数据。我想调用该 cfc 并在 div 中显示 ajax 响应。最终我想用 html 格式化响应。目前我无法显示响应。这是我到目前为止。

Here is the cfc : Asset.cfc

这是 cfc:Asset.cfc

<cffunction name="Asset" access="remote" returntype="array">
        <cfargument name="asset_id" type="string" required="yes">

         <!--- Define the local scope. --->
          <cfset var LOCAL = {} />    
          <cfset var qPics = "" />
          <cfset var result = arrayNew(1) />
          <cfset var PicStruct  = '' />

        <cfquery name="Pics">
        SELECT DISTINCT aq.ID
        FROM AAssignment a 
        INNER JOIN Assets aq ON aq.ID = a.Asset
        WHERE a.AssetItem = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.assetgrp_id#">                
        </cfquery>

         <cfloop query="Pics">
            <cfset PicStruct = StructNew() />
            <cfset PicStruct["value"] = ID />
            <cfset ArrayAppend(result,PicStruct) />
          </cfloop>

        <cfset myResult="#result#">
        <cfreturn myResult>
    </cffunction>

Here is the jquery

这是jquery

   <script>
    var caseid = <cfoutput>'#URL.ID#'</cfoutput>

    $.ajax({
          type:'GET',
          url:'Asset.cfc?method=Asset&returnformat=json',
          dataType: "json",
          data: {
              asset_id:     caseid,
            },
          success:function(data) {
            if(data) {   // DO SOMETHING     
                $('#picoutput').html(data);
            } else { // DO SOMETHING }
          }
        }
       });
    </script>

<div id="picoutput"></div>

Right now I get this response back from the cfc in Firebug.

现在我从 Firebug 的 cfc 得到这个响应。

[{"value":"3277C2B9-155D-D714-40143A59A8290252"}]

However it will not display in the div.

但是它不会显示在 div 中。

回答by Vitthal

Use data.value

使用 data.value

  success:function(data) {
    if(data) {   // DO SOMETHING     
        $('#picoutput').html(data[0].value);
    } else { // DO SOMETHING }
  }

回答by bipen

use datatype as HTML if you are sending response as html

如果您将响应作为 html 发送,请使用数据类型作为 HTML

$.ajax({
      type:'GET',
      url:'Asset.cfc?method=Asset&returnformat=json',
      dataType: "html",  //<---- here
      data: {
          asset_id:     caseid,
        },
      success:function(data) {
        if(data) {   // DO SOMETHING     
            $('#picoutput').html(data);
        } else { // DO SOMETHING }
      }
    }
   });