简体   繁体   中英

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

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

I've struggled with something like this too. Try this:

var ValueToSend = {'': jsonToSend };

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

Notice that I removed contentType and dataType and that the json string is enclosed in an object with an empty property name. This article helped: http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/ Also make sure you have the routing correctly working on the server; I'd recommend leaving the URL hard-coded until you're sure you've got everyting working so I'd replace url: '@Url.Action("Parse", "GetMeaning")' with the actual route.

问题是ASP.NET阻止了Json,因为它由于<b></b>标记而将其视为安全隐患。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM