簡體   English   中英

將帶有POST的JSON發送到服務器進行解析-為什么此Ajax調用失敗?

[英]Sending JSON with a POST to server for parsing - why does this ajax call fail?

因此,我將JSON字符串發送到服務器以通過jQuery ajax進行解析。 我從Glosbe字典的API獲取JSON。 在大多數情況下,它可以按預期運行,但是某些JSON會失敗。 有趣的是,在這些情況下,我會收到“內部服務器錯誤”,但是當我嘗試調試應用程序時,似乎甚至沒有調用控制器。 我不知道那怎么可能。

這是我的觀點:

        $.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);
        }
    });

控制器:

    [HttpPost]
    public JsonResult Parse(String ValueToParse)
    {

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

    }

失敗的示例Json:

{"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"}

我也為類似的事情而苦惱。 嘗試這個:

var ValueToSend = {'': jsonToSend };

$.ajax({
    url: '@Url.Action("Parse", "GetMeaning")',
    data: ValueToSend 
    type: 'POST',
    success:....

注意,我刪除了contentTypedataType ,並且json字符串包含在具有空屬性名稱的對象中。 本文對您有所幫助: http : //encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/還請確保您的路由在服務器上正確運行; 我建議您對URL進行硬編碼,直到您確定一切正常為止,因此我將url: '@Url.Action("Parse", "GetMeaning")'替換為實際路由。

問題是ASP.NET阻止了Json,因為它由於<b></b>標記而將其視為安全隱患。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM