1

ローカル pc に JSON サービスを展開しています。ASP.net/C# で作業しています。IE 9 で返されるデータは、フォーマッターとパーサーでチェックされた有効な JSON 応答です。

以下は、HTML ファイルで作成している JQuery 呼び出し (JQuery バージョン 1.7.1 を使用) です。

http://mylocalhostpc:2483/Portfolio.html

<script type="text/javascript">
$(document).ready(function () {

    $.ajax({
        url: "http://mylocalhostpc/JSONService/ServiceHandler.ashx?seed=ptr&cnt=2",
        contentType: "application/json; charset=utf-8",
        type: "GET",
        dataType: 'json',
        data: {},
        success: function (data) {
            if (data.results[0]) {
                var htmlText = data.results[0];
                var jsonObject = parseAndConvertToJsonObj(htmlText);
            } else {
                document.getElementById("footer-bottom").innerHTML = "Could not load the page.";
            }
        },
        error: function (xhr, status,thrownError) {
            switch (xhr.status) {
                case 200:
                    alert(xhr.status + ":- " + thrownError);
                    break;
                case 404:
                    alert('File not found');
                    break;
                case 500:
                    alert('Server error');
                    break;
                case 0:
                    alert('Request aborted: ' + !xhr.getAllResponseHeaders());
                    break;
                default:
                    alert('Unknown error ' + xhr.status + ":- " + thrownError);
            }
        }
    });
});

「Request aborted: true」というエラー メッセージが毎回表示されます。ただし、URLを確認すると:

http://mylocalhostpc/JSONService/Default.aspx?seed=ptr&cnt=2

次のデータは正常に返されました。

{ "Table" : [ { 
    "Product_Active" : true,
    "Product_DateAdded" : "/Date(1349352480000+0530)/",
    "Product_ISBN" : "9788179637494",
    "Product_Id" : 71,
    "Product_Price" : 45,
    "Product_Rating" : 5
  },
  { 
    "Product_Active" : true,
    "Product_DateAdded" : "/Date(1349352480000+0530)/",
    "Product_ISBN" : "9789350492536",
    "Product_Id" : 142,
    "Product_Price" : 150,
    "Product_Rating" : 5
  }
] }

web.config ファイルのコード

<urlMappings>
  <add url="~/Default.aspx" mappedUrl="~/ServiceHandler.ashx"/>

サービス ハンドラのコード ビハインド - ServiceHandler.ashx.cs

public class ServiceHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
    if (context.Request.QueryString["seed"] != null)
    {
        string searchstring = string.Empty;
        Products oProducts = new Products();
        context.Response.ContentType = "text/json";

        switch (context.Request.QueryString["seed"].ToString())
        {
            case "pra":
            // prODUCT aLL
                context.Response.Write(ProcessingRequest.Serialize(oProducts.getAllProducts("0", "")));
                break;
            case "ptr":
                // pRODUCTS tOP rATED
                searchstring = context.Request.QueryString["cnt"] == null ? "20" : context.Request.QueryString["cnt"].ToString();
                context.Response.Write(ProcessingRequest.Serialize(oProducts.getTopRatedProducts(searchstring)));
                break;
            default:
                context.Response.Write("Invalid service request, please check the url.");
                break;
        }
        context.Response.End();
    }
    else
    {
        context.Response.Write("Invalid service request, please provide valid seed.");
        context.Response.End();
    }
}
public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

どこで間違いを犯していますか、これを解決するのを手伝ってください?

dataType を 'jasonp' および 'script' としても使用しようとしましたが、エラーが 'Unknown error undefined' に変わります。

PS:さらに努力した後、次のエラーが発生しました:

私のjqueryエラーハンドラに以下のコードを追加しました

case 200: alert(xhr.status + ":- " + thrownError); break;

エラーコードを次のように返しました

200:- エラー: jQuery171049959372938610613_1356351917595 は呼び出されませんでした

4

2 に答える 2

0

このように使用する必要があると思います:

success: function (data) {
   $.each(data.Table, function(i, resp){
        if (resp) {
            var htmlText = resp;
            var jsonObject = jQuery.parseJSON(htmlText);
            console.log(jsonObject);
        } else {
            $("#footer-bottom").html("Could not load the page.");
        }
   });
},

あなたもこれで試すことができます:

success: function (data) {
   $.each(data.Table, function(i, resp){

            console.log(resp);

   });
},
于 2012-12-24T06:57:22.863 に答える