2013-05-20 45 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事件不会触发? –

相关问题