Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从asp.net c中的webservice读取javascript中的JsonResult#_Javascript_Asp.net_Ajax_Json_Web Services - Fatal编程技术网

从asp.net c中的webservice读取javascript中的JsonResult#

从asp.net c中的webservice读取javascript中的JsonResult#,javascript,asp.net,ajax,json,web-services,Javascript,Asp.net,Ajax,Json,Web Services,我试图用javascript读取json结果,但它给了我错误 它在var jsonObj=JSON.parse(msg)上给了我一个错误。 错误:未捕获的语法错误:意外标记o 当我调试代码时,Webservice返回完美的json数据,但它在ajax中的成功事件中给出错误。 我如何循环使用这个json对象?非常感谢您的帮助 <script type="text/javascript"> var ProductCategoryList; functi

我试图用javascript读取json结果,但它给了我错误

它在var jsonObj=JSON.parse(msg)上给了我一个错误。 错误:未捕获的语法错误:意外标记o 当我调试代码时,Webservice返回完美的json数据,但它在ajax中的成功事件中给出错误。 我如何循环使用这个json对象?非常感谢您的帮助

<script type="text/javascript">

        var ProductCategoryList;

        function callpageload() {
            $.ajax({
                type: "GET",
                url: "WebService1.asmx/GetCategoryList",
                contentType: "application/json; charset=utf-8",
                success: function (msg) {
                    var jsonObj = JSON.parse(msg);
                },
                error: function (msg) {

                }
            });
        }

</script>

var产品分类列表;
函数callpageload(){
$.ajax({
键入:“获取”,
url:“WebService1.asmx/GetCategoryList”,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(msg){
var jsonObj=JSON.parse(msg);
},
错误:函数(msg){
}
});
}
Web服务代码

public string GetCategoryList()
        {
            DataSet ds = Persistance.GetCategoryList();
            List<ProductCategories> prodlst = new List<ProductCategories>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ProductCategories prod = new ProductCategories();
                    prod.pid = ds.Tables[0].Rows[i]["pid"].ToString();
                    prod.id = ds.Tables[0].Rows[i]["id"].ToString();
                    prod.name = ds.Tables[0].Rows[i]["name"].ToString();
                    prodlst.Add(prod);
                }

            }
            string json = JsonConvert.SerializeObject(prodlst.ToArray());
            return json;
        }
公共字符串GetCategoryList()
{
DataSet ds=persistence.GetCategoryList();
List prodlst=新列表();
如果(ds.Tables[0].Rows.Count>0)
{
对于(int i=0;i
嘿,我也有类似的问题。对我来说,我也在使用JQuery

而不是你的

var jsonObj = JSON.parse(msg);
尝试使用

var jsonObj = $.parseJSON(msg);
我建议检查的另一件事是在c#end中放置一个断点,以确保在使用newtonsoft JSONconvert时没有异常或错误。当它返回一个空的json字符串时,这种情况会发生很多次

让我知道这是否有帮助

在我的代码中,有人执行了var JSON={}
因此它导致了一个错误。

尝试解析
msg.d

也使用POST;更安全。

什么错误?我没有看到任何错误。为您的
ajax
设置选项
dataType:'json'
。我认为您不需要解析响应,因为它已经是json格式。$.parseJSON使用json.parse(如果可用),所以。。。这不太可能有任何效果。Jquery版本适合我。所以我只是把我的经验贴在这里。