与后向服务器发送解析JSON - 为什么会发生这种Ajax调用失败? [英] Sending JSON with a POST to server for parsing - why does this ajax call fail?

查看:116
本文介绍了与后向服务器发送解析JSON - 为什么会发生这种Ajax调用失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我送一个JSON字符串到我的服务器通过jQuery的AJAX解析。我从glosbe字典的API的JSON。它的大部分工作正常,但也有失败的某些JSONs的时间。有趣的是,在这些情况下,我得到一个内部服务器错误,但是当我试图调试应用程序,这似乎是控制器甚至没有叫。我不知道怎么说甚至有可能。

下面是我的看法:

  $。阿贾克斯({
        网址:@ Url.Action(解析,GetMeaning),
        数据类型:JSON,
        数据:ValueToParse =+ jsonToSend,//也许是更好地发送一个字符串的JSON而不是
        键入:POST,
        异步:假的,
        的contentType:应用/ JSON的;字符集= UTF-8,
        成功:功能(数据){
            //将数据传递到服务器进行解析

            //警报(sendJsonToServer成功sended+ JSON.stringify(json1)+回来:+数据);
            setMeaningofEx pression(如pression,数据);
            $('#EX pressionTranslations)HTML(如pressionsAsString())。
        },
        错误:函数(XHR,状态,错误){
            警报('sendJsonToServer错误,试图向其发送+ JSON.stringify(jsonToSend)+'的问题:+状态++误差);
        }
    });
 

控制器:

  [HttpPost]
    公共JsonResult解析(字符串ValueToParse)
    {

            返回JSON(Parser.Parse(ValueToParse).ToArray());

    }
 

这是失败的一个例子的Json:

<$p$p><$c$c>{"result":"ok","authors":{"1":{"U":"//en.wiktionary.org","id":1,"N":"en.wiktionary.org"},"20":{"U":"//www.slowniki.org.pl/","id":20,"N":"Jerzy Kazojc"},"86":{"U":null,"id":86,"N":"wiki"},"25018":{"U":"//glosbe.com","id":25018,"N":"GlosbeResearch"},"2695":{"U":"//dumps.wikimedia.org/enwiktionary/latest/enwiktionary-latest-pages-articles.xml.bz2","id":2695,"N":"Wiktionary"}},"dest":"hun","phrase":"cat","tuc":[{"authors":[86],"meaningId":-4164218657921168000,"meanings":[{"text":"member猫科动物,语言:ENG},{文:国内种,语言:ENG},{文:一个常见的​​四条腿的动物(猫属西尔韦斯特里斯),它是经常保持作为一个家庭pet.","language":"eng"},{"text":"ket","language":"eng"}],"phrase":{"text":"macska","language":"hun"}},{"authors":[2695],"meaningId":-9072797188187576000,"meanings":[{"text":"domestic species","language":"eng"}],"phrase":{"text":"kandúr","language":"hun"}},{"authors":[1],"meaningId":4566464096976442400,"meanings":[{"text":"domestic species","language":"eng"}],"phrase":{"text":"cica","language":"hun"}},{"authors":[25018],"meaningId":3489096155947903000,"phrase":{"text":"hány","language":"hun"}},{"authors":[25018],"meaningId":-557814434056432960,"phrase":{"text":"krapek","language":"hun"}},{"authors":[20],"meaningId":-2600640194269463000,"phrase":{"text":"muksó","language":"hun"}},{"authors":[25018],"meaningId":-4572067174236314000,"phrase":{"text":"rókázik","language":"hun"}},{"authors":[1],"meaningId":null,"meanings":[{"text":"An爱好者或玩家爵士乐,语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:(计算)在lsquo的;链状;在Unix编程和命令,读取一个或多个文件,并指示他们的内容到输出装置,语言:ENG}]},{作者:[1],meaningId:空的含义:[{文:(俚语,粗俗,非洲裔美国黑人)阴道;女性外生殖器,语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:(俚语)吐的东西。 ,语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:(爱尔兰,非正式​​的)可怕的,灾难性的,语:ENG}]},{作家:[1],meaningId:空,意义:[{文:(陈旧,不可数)的游戏;陷阱球; (也称,猫,狗),语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:(。俚语)任何各种土方机械。 (从制造商卡特彼勒公司),语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:养锚猫头,语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:(航海)鞭打用cat-o-nine-tails.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"vomit","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(archaic)一个坚固的商船帆船(目前只在,独桅艇;),语言:ENG}]},{作家:[1],meaningId:空,意义:[{文本:(俚语)妓女。 [至少从早期的15℃。],语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:任何类似动物家庭猫科动物,包括狮子,老虎等,语言:ENG}]},{作家:[1],meaningId:空,意义:[{文本:(航海)收缩猫邻九尾巴,语言:ENG}]},{作家:[1],meaningId:空,意义:[{ 文:帅哥,老乡,语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:(古代,游戏的不可数)陷阱,陷阱和球;,语言:ENG}]},{作家:[1],meaningId:空,意义:[{文本:(俚语)的人(通常是男性),语言:ENG}]},{作家:[1],meaningId:空,意义:[{文本 :(海里)在升降(锚)通过其环,以便它挂在猫头。,语言:ENG}]},{作者:[1],meaningId:空, 意义:[{文:(计算俚语)转储大量的(一个未prepared目标)的数据通常不打算仔细浏览吧。,语言:ENG} ]},{作家:[1],meaningId:空,意义:[{文:(贬义)赌气或愤怒的女人。 [从早期13℃。],语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:鲶鱼 ,语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:家养亚种猫科动物(猫属西尔韦斯特里斯猫)通常保留作为宠物。 [从8角],语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:(航海)的强滑车用来吊锚船舶的猫头,语言:ENG}]},{作家:[1],meaningId:空,意义:[{文本 :(计算)申请的&lt; B&GT;猫&LT; / B&GT;命令(文件),语言:ENG}]},{作家:[1],meaningId:空,意义:[{文:强滑车用葫芦锚船舶,语言的猫头:ENG}]},{作家:[1],meaningId:空,意义:[{文:双体船从ENG,}]}]:,语言:ENG}

解决方案

但问题是,ASP.NET阻止JSON的,因为它认为这是因为存在安全隐患的&LT; B&GT;&LT; / B&GT; 标签

So, I send a JSON string to my server for parsing via jQuery ajax. I get the JSON from the Glosbe dictionary's API. Most of the time it works as expected, but there are certain JSONs that fail. What is interesting is that in those cases I get an "Internal server error", but when I tried to debug the application, it seemed like the controller wasn't even called. I don't know how that's even possible.

Here's my View:

        $.ajax({
        url: '@Url.Action("Parse", "GetMeaning")',
        dataType: "json",
        data: "ValueToParse=" + jsonToSend, //maybe it's better to send a json instead of a string
        type: 'POST',
        async: false,
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            //pass the data to the server for parsing

            //alert("sendJsonToServer successful, sended " + JSON.stringify(json1) + "got back: " + data);
            setMeaningofExpression(expression, data);
            $('#expressionTranslations').html(expressionsAsString());
        },
        error: function (xhr, status, error) {
            alert('sendJsonToServer error, tried to send ' + JSON.stringify(jsonToSend) + 'problem: ' + status + " " + error);
        }
    });

Controller:

    [HttpPost]
    public JsonResult Parse(String ValueToParse)
    {

            return Json(Parser.Parse(ValueToParse).ToArray());

    }

An example Json that fails:

{"result":"ok","authors":{"1":{"U":"//en.wiktionary.org","id":1,"N":"en.wiktionary.org"},"20":{"U":"//www.slowniki.org.pl/","id":20,"N":"Jerzy Kazojc"},"86":{"U":null,"id":86,"N":"wiki"},"25018":{"U":"//glosbe.com","id":25018,"N":"GlosbeResearch"},"2695":{"U":"//dumps.wikimedia.org/enwiktionary/latest/enwiktionary-latest-pages-articles.xml.bz2","id":2695,"N":"Wiktionary"}},"dest":"hun","phrase":"cat","tuc":[{"authors":[86],"meaningId":-4164218657921168000,"meanings":[{"text":"member of Felidae","language":"eng"},{"text":"domestic species","language":"eng"},{"text":"A common four-legged animal (Felis silvestris) that is often kept as a household pet.","language":"eng"},{"text":"ket","language":"eng"}],"phrase":{"text":"macska","language":"hun"}},{"authors":[2695],"meaningId":-9072797188187576000,"meanings":[{"text":"domestic species","language":"eng"}],"phrase":{"text":"kandúr","language":"hun"}},{"authors":[1],"meaningId":4566464096976442400,"meanings":[{"text":"domestic species","language":"eng"}],"phrase":{"text":"cica","language":"hun"}},{"authors":[25018],"meaningId":3489096155947903000,"phrase":{"text":"hány","language":"hun"}},{"authors":[25018],"meaningId":-557814434056432960,"phrase":{"text":"krapek","language":"hun"}},{"authors":[20],"meaningId":-2600640194269463000,"phrase":{"text":"muksó","language":"hun"}},{"authors":[25018],"meaningId":-4572067174236314000,"phrase":{"text":"rókázik","language":"hun"}},{"authors":[1],"meaningId":null,"meanings":[{"text":"An enthusiast or player of jazz.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(computing) A lsquo;catenate; program and command in Unix that reads one or more files and directs their content to an output device.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(slang, vulgar, African American Vernacular) A vagina; female external genitalia","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(slang) To vomit something.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(Irish, informal) terrible, disastrous.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(archaic, uncountable) The game of ;trap and ball; (also called ;cat and dog;).","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(slang) Any of a variety of earth-moving machines. (from their manufacturer Caterpillar Inc.)","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"raise anchor to cathead","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(nautical) To flog with a cat-o-nine-tails.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"vomit","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(archaic) A sturdy merchant sailing vessel (now only in ;catboat;).","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(slang) Prostitute. [from at least early 15th c.]","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"Any similar animal of the family Felidae, which includes lions, tigers, etc.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(nautical) Contraction of cat-o-nine-tails.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"guy, fellow","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(archaic, uncountable) The trap of the game of ;trap and ball;.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(slang) A person (usually male).","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(nautical) To hoist (the anchor) by its ring so that it hangs at the cathead.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(computing slang) To dump large amounts of data on (an unprepared target) usually with no intention of browsing it carefully.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(derogatory) A spiteful or angry woman. [from earlier 13th c.]","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"A catfish.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"A domesticated subspecies (Felis silvestris catus) of feline animal, commonly kept as a house pet. [from 8th c.]","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(nautical) A strong tackle used to hoist an anchor to the cathead of a ship.","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"(computing) To apply the <b>cat</b> command to (a file).","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"strong tackle used to hoist an anchor to the cathead of a ship","language":"eng"}]},{"authors":[1],"meaningId":null,"meanings":[{"text":"A catamaran.","language":"eng"}]}],"from":"eng"}

解决方案

The problem was that ASP.NET blocked the Json because it considered it a security risk because of the <b></b> tags.

这篇关于与后向服务器发送解析JSON - 为什么会发生这种Ajax调用失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆