2013-05-20 46 views
1
Error - Error: Uncaught SyntaxError: Unexpected token : 

exact issue error
平臺 - Icenium。劍道UI - 錯誤:未捕獲的SyntaxError:意外的標記:

我們正在使用Remote Service - http://localhost:35798/RestServiceImpl.svc/json來獲取數據。我附加了從服務收到的數據格式。 return format of data

這裏是我的代碼:

var dataSource = new kendo.data.DataSource({ 
    schema: { 
     data: "d" 
    }, 
    transport: { 
     read: { 
      url: "http://localhost:35798/RestServiceImpl.svc/json", 
      dataType: "jsonp", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests 
      data: { 
       id: "4" 
      }, 
      type: "GET", 
      contentType: "application/json;charset=utf-8" 
     }, 
     change: function() { 
      alert('called'); 
      var data = this.data(); 
      console.log(data.length); // displays "77" 
      debugger; 
      $('#txtJson').val(data[0].name); 
     } 

    } 
}); 

$("#submitButton").click(function() { 
    dataSource.read(); 
    var data = dataSource.data(); 
    console.log(data.length); 
}); 

這裏是我的服務代碼 -

[OperationContract] 
    [WebInvoke(Method = "GET", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "json?id={id}") 
     ] 
    List<Person> JSONData(string id); 

,Q 1)如何解決這個錯誤 - Uncaught SyntaxError: Unexpected token,我失去的東西嗎?

問2)點擊按鈕後,我打電話dataSource.read(),之後dataSource.data().length越來越0.我認爲這應該在dataSource.bind(change:function())處理。但是,dataSource.read()更改功能不會觸發。

+0

您不包括圖像! – OnaBai

+0

我已添加圖片。我希望你清楚我在找什麼。我還指定了圖像中從服務返回的數據。謝謝, –

+0

你是否意識到你沒有返回JSONP,而是一個JSON?這是「意外令牌」錯誤的原因。 – OnaBai

回答

1

一旦你解決了JSON vs JSONP的問題,數據仍然是空的,因爲你並不是在datasource.schema中說數據實際上在接收到的JSON的一個元素中,它叫做JSONDataResult

schema應該是:

schema: { 
    data: "JSONDataResult" 
}, 

您可以添加解析功能架構進行調試你會得到什麼:

schema : { 
    parse: function(response) { 
     console.log("parse"); 
     console.log(JSON.stringify(response, null, 4)); 
     debugger; 
     return response.JSONDataResult; 
    } 
} 
+0

我已經嘗試過架構,但沒有運氣,長度是未定義的。在dataSource._pristine屬性下,我確實看到了我的數據。這裏可能是什麼問題? http://imageshack.us/f/826/lengthundefined.png/ –

+0

嘗試定義一個'schema.parse'函數來顯示你接收到的內容。看我的編輯。 – OnaBai

+0

它工作得很好。謝謝 :)。 之後,dataSource.read(),架構解析函數被激發,然後它確實stringify,然後我得到dataSource.data()中的數據。 最後一個問題 - 在dataSource.read()之後,爲什麼datasource的change事件不會觸發? –

相關問題