2013-11-03 115 views
0

我有以下的web服務,其中,通過asp.net進行測試時,「視圖瀏覽器」不會檢索它顯示了在XML瀏覽器Web服務沒有返回JSON

這裏的數據是Web服務

Imports System.Web 
Imports System.Web.Services 
Imports System.Web.Script.Services 
Imports System 
Imports System.IO 
Imports Newtonsoft.Json 
Imports System.Text 

<ScriptService()> _ 
<WebService(Namespace:="BATLDataRetrieval")> _ 
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ 
Public Class WebService 
Inherits Services.WebService 

    <WebMethod()> _ 
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _ 
Public Function GetOhlcvData(ByVal symbol As String) As List(Of StockPricesDTO) 
    Dim tempList As New List(Of StockPricesDTO) 
    Dim stockData As New ArrayList() 

    Using ctx As New BATLEntities() 
     Dim symid As Long = (From sym In ctx.last60dayssymbols Where symbol = sym.symbol Select sym.id).FirstOrDefault() 
     Dim data = (From ohlcv In ctx.last60daysdata 
        Where ohlcv.lastSixty_symbolId = symid 
        Select ohlcv).ToList() 

     For Each dataDay In data 
      Dim tempSp As New StockPricesDTO 
      tempSp.QuoteDate = DateTimeToUnixTimestamp(dataDay.date) 
      tempSp.Open = dataDay.open 
      tempSp.High = dataDay.high 
      tempSp.Low = dataDay.low 
      tempSp.LastSale = dataDay.last 
      tempSp.Volume = dataDay.volume 
      tempList.Add(tempSp) 
     Next 
     Return tempList 
    End Using 
End Function 

我使用這個服務檢索stockdata從我們的數據庫使用如下 ,我需要它以JSON用此格式返回數據

[[1162512000000,79.36,79.53,77.79,78.29,15426335], 
[1162771200000,78.95,80.06,78.43,79.71,15525782], 
[1162857600000,80.45,81.00,80.13,80.51,18788494]....] 

的每Fiddler傳回實際JSON是

{"d": [{"__type":"StockPricesDTO","QuoteDate":1383282000000,"Open":1031.79,"High":1036.00,"Low":1025.10,"LastSale":1027.04,"Volume":1283300},{"__type":"StockPricesDTO","QuoteDate":1383195600000,"Open":1028.93,"High":1041.52,"Low":1023.97,"LastSale":1030.58,"Volume":1616400},{"__type":"StockPricesDTO","QuoteDate":1383109200000,"Open":1037.43,"High":1037.51,"Low":1026.00,"LastSale":1030.42,"Volume":1324100}, 

JavaScript代碼是

$(function() { 
     var symbol = "GOOG"; //will replace with <a> tag click value 
     $.ajax({ 
      type: "POST", 
      url: "WebService.asmx/GetOhlcvData", 
      contentType: "application/json; charset=utf-8", 
      data: '{symbol: "' + symbol + '"}', 
      cache: false, 
      dataType: "json", 
      success: function (data) { 
       alert(data.d); 
       showChart(data.d); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       alert(textStatus + ' - ' + errorThrown); 
      } 
     }); 
     function showChart(chartData) { 
      $('#chartContainer').highcharts('StockChart', { 
       rangeSelector: { 
        selected: 1 
       }, 
       title: { 
        text: symbol + ' Stock Price' 
       }, 
       series: [{ 
        name: symbol, 
        data: chartData, 
        tooltip: { 
         valueDecimals: 2 
        } 
       }] 
      }); 
     } 
    }); 

警報(data.d)產生的,而不是在JSON實際數據[對象的對象。這是我第一次嘗試創建一個返回json的服務,所以請原諒我的無知,如果這是一項簡單的任務。

+0

如果使用chrome工具或螢火蟲進行調試,'data'內的數據看起來像什麼? – christiandev

+0

在小提琴手它顯示每個數組數組中的第一項作爲_type = StockPricesDTO ....是什麼導致這個問題?看到上面的編輯 – dinotom

回答

0

我會嘗試更換

data: '{symbol: "' + symbol + '"}', 

data: {symbol: symbol}, 

你會看到更多的(使用警報()的時候比),如果你

  • 輸出到控制檯console.log(data)
  • 不使用MSIE,但Chrome,Firefox,...和他們的網站-Developer utitilies
  • 看在Chrome Webinspector/Firefox的Firebug的網絡選項卡,並分析請求和響應

在控制檯(或調試器),你可以檢查什麼是由服務器返回,並告訴我們,如果你不會跟它一起來的。

+0

0:對象 高:1036 LastSale:1027.04 低:1025.1 開:1031.79 QuoteDate:13832.82億 量:1283300 __type: 「StockPricesDTO」 是Chrome控制檯顯示作爲回報 – dinotom

+0

如果highchart真正需要以[[n,n,n],[n,n,n],...]形式存在的數據,您必須轉換數據服務器端或客戶端。我會做客戶端。如果你可以依靠ES5,這只是一些'Array.map'調用的問題。你需要幫助嗎?我的答案如何 - 是否有幫助? – hgoebl

+0

接受的答案,因爲它引導我走下去,最終與我一起使用arraylist並返回數據 – dinotom