【问题标题】:server side response is text/html服务器端响应是 text/html
【发布时间】:2013-11-12 13:44:10
【问题描述】:

服务器端响应是 text/html,但我希望它是 application/json,我做错了什么?

$.ajax({
        type:'GET',
        url: 'http://pda.bilgiteknolojileri.net',
        dataType: 'json',
        contentType: "application/json",
        crossDomain: true,
        success: function(categories) {
            $.each(categories.data, function(i, data) {
                var cat=data.cat;
                var cat_id=data.cat_id;
                $('#category').append('<option value="'+cat_id+'">'+cat+'</option>');
            });
        }
    });

响应头:

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/7.5
Set-Cookie: CFID=361332;expires=Thu, 05-Nov-2043 13:44:48 GMT;path=/
Set-Cookie: CFTOKEN=7dc38bfc66b716c4-FD851F26-DBE0-51F0-FA966E8B326FCC26;expires=Thu, 05-Nov-2043 13:44:48 GMT;path=/
Set-Cookie: JSESSIONID=7030cdc9717e7f40c8d12926443811504160;path=/
Set-Cookie: WRK_COOKIE_ID=FD851F82%2DB9BE%2DCFB8%2D76D63257CF3F256B;expires=Thu, 05-Nov-2043 13:44:48 GMT;path=/
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET
Date: Tue, 12 Nov 2013 13:44:47 GMT

服务器端标头响应代码开头为:

<html>
    <head></head>
    <body>

        <pre style="word-wrap: break-word; white-space: pre-wrap;">

                {"status":"ok","count":351,"data":[

和服务器端代码:

<html>
    <head></head>
    <body>
        <cfif isdefined('attributes.cat_id')>
            <cfquery name="GET_PRODUCT_CATS" datasource="#DSN3#">
                    SELECT PRODUCT_CAT,IS_CUSTOMIZABLE,HIERARCHY FROM PRODUCT_CAT WHERE PRODUCT_CATID = #attributes.cat_id#
            </cfquery>
        </cfif>
        <cfquery name="get_json" datasource="#dsn3#">
            SELECT P.PRODUCT_ID,P.PRODUCT_NAME,PC.PRODUCT_CAT,PC.PRODUCT_CATID,PR.PRICE,PR.MONEY,PC.HIERARCHY
            FROM PRODUCT_CAT PC 
            LEFT OUTER JOIN PRODUCT P ON P.PRODUCT_CATID=PC.PRODUCT_CATID
            LEFT OUTER JOIN PRICE PR ON P.PRODUCT_ID=PR.PRODUCT_ID
            WHERE P.IS_SALES=1 AND P.IS_EXTRANET=1 AND PR.PRICE IS NOT NULL
                <cfif isdefined('attributes.cat_id') and len(attributes.cat_id)>
                    AND (PC.PRODUCT_CATID = <cfqueryparam value="#attributes.cat_id#" cfsqltype="cf_sql_integer"> OR PC.HIERARCHY LIKE <cfqueryparam value="#GET_PRODUCT_CATS.HIERARCHY#.%" cfsqltype="cf_sql_varchar"> )
                </cfif>
                <cfif isdefined('attributes.hierarchy')>AND PC.HIERARCHY=#attributes.hierarchy#</cfif>
            GROUP BY PC.HIERARCHY,PC.PRODUCT_CAT,P.PRODUCT_ID,P.PRODUCT_NAME,PC.PRODUCT_CATID,PR.PRICE,PR.MONEY
            ORDER BY PC.HIERARCHY,P.PRODUCT_NAME
        </cfquery>
        <cfset row=0>
        <cfoutput query="get_json" group="product_catid">
            <cfoutput group="product_id">
                <cfset row++/>
            </cfoutput>
        </cfoutput>
        <pre style="word-wrap: break-word; white-space: pre-wrap;">
            <cfif get_json.recordcount>
                {"status":"ok","count":<cfoutput>#row#</cfoutput>,"data":[
                <cfoutput query="get_json" group="product_catid">
                    {"cat":#product_cat#,"cat_id":#product_catid#,"products":[
                        <cfoutput group="product_id">
                            {"id":#product_id#,"name":#product_name#,"price":#tlformat(price,2)#,"currency":#money#}
                        </cfoutput>
                    ]}
                </cfoutput>
                ]}
            <cfelse>
                {"status":"error"}
            </cfif>
        </pre>
    </body>
</html>

【问题讨论】:

  • 请发布您的服务器端代码。响应头设置在服务器端。
  • 删除 &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;pre&gt; 和所有其余的 html 标签。我不是冷融合专家,但谷歌说要以这种方式设置标题&lt;cfcontent type="application/json; charset=UTF-8"&gt;
  • 有心,删除最后的&lt;/pre&gt;标签。
  • 仔细检查您的 json 数据。您肯定忘记在类别之间插入逗号。
  • 是的,只是语法错误:)

标签: jquery ajax json api coldfusion


【解决方案1】:

您需要将服务器代码设置为 Content-Type: application/json 标头,因此请使用 the cfheader tag(链接适用于 CF 8)。例如:

<cfheader name="Content-Type" value="application/json">

【讨论】:

  • 没问题,为什么我还没有得到数据?
  • 根据有问题的 cmets,这不是一个完整的答案
  • @JamesMohler - 我很困惑。您的问题是我没有提到 JSON 语法错误的修复吗? (直到我发布此答案后才发现。)
猜你喜欢
  • 2017-01-07
  • 2021-01-26
  • 2018-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 2017-07-25
相关资源
最近更新 更多