无法将数据从 c#(控制器) 发送到 javascript

我的JavaScript文件上有这个代码:


temp="string";

var myJson = JSON.stringify(temp);

    $.ajax(

        {

            url: '/MemoryGame/updateStatus',

            type: 'POST',

            contentType: "application/json; charset=utf-8",

            dataType: "json",

            data: myJson,

            success: function (response) {

                alert("success");

                if (response == 'Okay') {

                    checkStatus(temp.myID);

                }

               else {

                    ConnectionChanged();

                }

            },

            error: function (errorThrown) {

                console.log(errorThrown);

                ConnectionChanged();

            }

        });

这个控制器:


[HttpPost]

        public string updateStatus(string updatedJson)

        {


            var Player = JsonConvert.DeserializeObject<GameDataClass>(updatedJson);

            var Opponent = JsonConvert.DeserializeObject<GameDataClass>(System.IO.File.ReadAllText(System.IO.Path.Combine(_env.WebRootPath, Player.OpponentID + ".json"))); 

... }

我试图将$.ajax更改为$.post方法,也改变了


公共字符串更新状态



public JsonResult updatestatus


但这两个都没有奏效。javascript上的myJson包含数据,但是当它到达控制器更新时Json是空的。我从来没有这样的经验,所以我正在使用另一个项目的代码,它在那里工作得很好。那么有人可以建议我做错了什么吗?


三国纷争
浏览 120回答 2
2回答

杨魅力

&nbsp; temp="string";&nbsp; &nbsp; // (0)&nbsp; &nbsp; var myJson = JSON.stringify(temp);&nbsp; &nbsp; $.ajax(&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '/MemoryGame/updateStatus?updatedJson=' + temp, // (1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType: "application/json; charset=utf-8",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: '',&nbsp; &nbsp;// (2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("success");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response == 'Okay') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkStatus(response.myID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConnectionChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function (errorThrown) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConnectionChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });或者,如果这不必作为参数传递,请执行以下操作:(0) var formData = new FormData();formData.append('updatejson', temp);(1) url: '/MemoryGame/updateStatus', (2) data: formData,

浮云间

$.ajax&nbsp;是 jQuery 库中的一个词库,你的项目包含它吗?您还可以检查浏览器的Javascript控制台,看看它是否包含错误。在Firefox和Chrome上,您可以通过按F12来访问它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript