服务器端响应是text / html

时间:2013-11-12 13:44:10

标签: jquery ajax json api coldfusion

服务器端响应是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>

1 个答案:

答案 0 :(得分:1)

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

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