日期:2014-05-16  浏览次数:20374 次

jquery 返回json
调用 服务器webservice返回的值是这样子:

 <?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="mytest/">{ "dd":[ { "f_id":"1","f_name":"王一"}, 
       { "f_id":"2","f_name":"王二"},
       { "f_id":"3","f_name":"王三·"}
      ]}</string> 

json数据外面被自动加了一层xml,貌似不能去掉
//接收js
<script>
$(function(){
$('button').click(function(){
$.ajax({  
                url: "http://localhost/anserver/appws.asmx/GetUserList", 
                type: "POST",   
                data: {a:1,b:1}, 
                contentType: "application/json; charset=utf-8", 
                dataType: 'json',
                success: function(result) {  
                    alert(result); 
                },  
                error: function(e) {  
                    alert("错误"+e.responseText);   
                }  
            }); 
             
 });
});
</script>
我想的是直接得到json数据,目前提示  、、system.invalidoperationException:请求的格式无效:application/json; charset=utf-8


------解决方案--------------------
既然服务器返回的是xml,那么只能用xml来解析
dataType: 'xml'

然后从xml内部取得json字符串:
success: function(result) {  
    var json = $(result).find('string').text(); 
    result = $.parseJSON(json);
}

要么另一种方案就是修改服务器的输出
------解决方案--------------------
contentType: "application/json; charset=utf-8", 

需要framework是3.5+的,你的framework版本对了没