在回发的AJAX调用一个WCF Web服务使用POSTDATA但JSON.stringify上失败主要是IE浏览器,虽然在Chrome和Firefox正常工作主要是、浏览器、正常、工作

2023-09-10 20:36:20 作者:゛你是我无法遗失的美好ゝ

我有一个Dynatree和调用onSelect:功能(选择节点),我所说的例行SaveUserTree()`

在这个程序中,我得到了最新的 toDict()上,然后传递给一个JSON字符串化功能树。所得POSTDATA然后被发送到WCF服务呼叫。 但是,JSON.stringify失败的IE浏览器,但工作在两种Chrome和Firefox。 该错误是未定义,并使用F12调试其他一些信息显示:

  

JSON未定义   数-2146823279   命名类型错误

 函数SaveUserTree(treeNode节点){
    VAR字典= $(#树)dynatree(getTree)toDict()。

    POSTDATA = JSON.stringify(字典);

    $阿贾克斯({
        键入:POST,
        网址:。?TodoTreeService.svc / SaveTree /新名称=+ $(#hdnUserFile)VAL()
        //的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        数据:POSTDATA,
        成功:函数(ARG){
            警报(成功 - + JSON.stringify(ARG));
        },
        错误:函数(XHR,ajaxOptions,thrownError){
            警报(错误 - + JSON.stringify(xhr.responseText));
        }
    });

}
 

请帮忙,因为这是一个大问题。

解决方案 抛弃WebService,在.NET4中用 jQuery 调用 WCF

这将是必然,因为你使用的是IE浏览器的旧版本不支持JSON。所以,使用 json2.js 后备由Douglas Crockford的 json3.js 回落。只是参考图书馆在你的网页像这样

 <脚本类型=文/ JavaScript的
        SRC ='// cdnjs.cloudflare.com/ajax/libs/json3/3.3.0/json3.min.js'>
< / SCRIPT>
 

I have a Dynatree and onSelect: function(select, node), I call a routine SaveUserTree()`.

In this routine, I get the latest toDict() on the tree which then is passed to a JSON stringify function. The resultant postData is then sent to the WCF service call. However, the JSON.stringify fails on IE but works in both Chrome and FireFox. The error is 'undefined' and some other info using F12 debugger reveals:

JSON is undefined number -2146823279 name "TypeError"

function SaveUserTree(treeNode){
    var dict = $("#tree").dynatree("getTree").toDict();

    postData = JSON.stringify(dict);

    $.ajax({
        type: "POST",
        url: "TodoTreeService.svc/SaveTree/New?Name=" + $("#hdnUserFile").val(),
        //contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: postData, 
        success: function (arg){
            alert("SUCCESS - " + JSON.stringify(arg));                   
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("ERROR - " + JSON.stringify(xhr.responseText));
        }  
    });

}

Please help as this is a biggie.

解决方案

This will be surely because you are using older version of IE which does not support JSON. So use the json2.js fallback by Douglas Crockford json3.js fallback. Just refer the library in your page like this

<script type='text/javascript' 
        src='//cdnjs.cloudflare.com/ajax/libs/json3/3.3.0/json3.min.js'>
</script>