2012-03-27 58 views
0

工作,我有一个Web服务,其以JSON格式返回数据内容类型在jQuery的

[WebMethod] 
     public string GetCustomers() 
     { 
      using (var documentStore = new DocumentStore { Url = "http://haseeb-pc:8080/" }.Initialize()) 
      { 
       using (var session = documentStore.OpenSession()) 
       { 
        var query = session.Query<Customer>().Select(customer => customer).Take(20); 
        var serializer = new JavaScriptSerializer(); 
        return serializer.Serialize(query); 
       } 
      } 
     } 

在jQuery中,当我使用:

contentType: "application/json; charset=utf-8", 

我的萤火得到一个错误:

{"Message":"Invalid JSON primitive: take.","StackTrace":........................ 

当我删除此:

contentType: "application/json; charset=utf-8", 

我的web服务给了我这样的XML:

<string xmlns="http://tempuri.org/"> 
[{"FirstName":"Human1","LastName":"Being1","Email":"myemail1","ProductInfo":{"Name":"product1","Quantity":10}}] 
</string> 

但是我希望结果是JSON?

回答

0

当使用jQuery的$ .ajax方法进行AJAX请求时,您不应该需要提供dataType而不是contentType吗?

的contentType用于特定格式的数据发送到服务器

数据类型用于接收来自服务器的数据,具体格式

像这样

$.ajax({ 
    type: "GET", 
    url: "test.js", 
    dataType: "json" 
});