简体   繁体   English

如何将JSON字符串化的JavaScript对象发送到服务器

[英]How to send a JSON stringified javaScript object to the server

I'm using VB.Net and MVC 5. I have a an object I am creating with javaScript like this: 我正在使用VB.Net和MVC5。我有一个用javaScript创建的对象,如下所示:

        var myEdits = {
            listOfIDs: [],
            listOfValues : []
        };

I need to send this object to my controller and move on to the next view with the information it contains. 我需要将该对象发送到控制器,并使用其包含的信息移至下一个视图。

I can successfully stringify the object and pass it to the controller via ajax, and manipulate the data, but this does not allow me to render the new view on success. 我可以成功地对对象进行字符串化,并通过ajax将其传递给控制器​​,并处理数据,但这不允许我在成功时呈现新视图。

I tried to use window.location and endodeURIComponent like this: 我试图像这样使用window.locationendodeURIComponent

            myEdits = encodeURIComponent(JSON.stringify(myEdits));

            var postString = ("/ViewDetails/EditConfirmation/" + myEdits);

            window.location = postString;

But I receive this error still: 但是我仍然收到此错误:

A potentially dangerous Request.Path value was detected from the client (:). 从客户端(:)检测到潜在的危险Request.Path值。

Which I find odd, because I cannot see any :'s in the request: 我觉得很奇怪,因为在请求中看不到任何::

EditConfirmation/%7B"listOfIDs"%3A%5B"22"%2C"23"%2C"24"%2C"25"%2C"26"%2C"27"%2C"28"%2C"29"%2C"30"%2C"31"%2C"32"%2C"33"%2C"34"%2C"35"%2C"36"%5D%2C"listOfValues"%3A%5B""%2C""%2C""%2C""%2C""%2C""%2C""%2C""%2C""%2C""%2C"Yes"%2C"Yes"%2C"Yes"%2C"Yes"%2C"No"%5D%7D EditConfirmation /%7B “listOfIDs” %3A%5B “22” %2C “23” %2C “24” %2C “25” %2C “26” %2C “27” %2C “28” %2C “29” % 2C “30” %2C “31” %2C “32” %2C “33” %2C “34” %2C “35” %2C “36” %5D%2C “listOfValues” %3A%5B “” %2C” “%2C”, “%2C”, “%2C”, “%2C”, “%2C”, “%2C”, “%2C”, “%2C”, “%2C” 是 “%2C” 是 “%2C” 是“%2C “是的” %2C “否” %5D%7D

What is the proper way to pass this object via javaScript or jQuery to the controller, and have the server render the new view? 通过javaScript或jQuery将此对象传递给控制器​​,并使服务器呈现新视图的正确方法是什么?

It is getting URL encoded because you're using an HTTP GET. 由于您使用的是HTTP GET,因此正在对URL进行编码。 If you're sending lots of info like this, you likely want to be using an HTTP POST. 如果您要发送大量这样的信息,则可能要使用HTTP POST。 See jQuery's $.ajax method. 参见jQuery的$ .ajax方法。

If you really wanted to continue using a GET rest assured that if your action took in a string parameter, it will come through as expected with the colons. 如果您确实想继续使用GET,请放心,如果您的操作采用了字符串参数,则冒号将按预期通过。 The : is getting encoded to %3A (see: http://www.w3schools.com/tags/ref_urlencode.asp for all of them). :已被编码为%3A (有关全部信息,请参见: http//www.w3schools.com/tags/ref_urlencode.asp )。

I solved this issue by placing my JSON string into a session variable with an AJAX call and then calling the action for the proper view on success using window.location . 我通过使用AJAX调用将JSON字符串放入会话变量中,然后使用window.location调用操作以获取success的正确视图来解决此问题。 I then retrieve the string from the session when the 'success' action is called to set up my model for the view. 然后,当调用“成功”操作为视图设置模型时,我从会话中检索字符串。

probably the dangerous request issue showed up because of trying to inject url path '/ViewDetails/EditConfirmation/' + myEdits into the browser. 可能由于尝试将URL路径'/ViewDetails/EditConfirmation/' + myEdits注入浏览器而出现了危险的请求问题。

Model Class 型号类别

public class myEdits
{
    public List<int> listOfIds {get;set;}
    public List<string> listOfValues {get;set;}
}

Javascript Part(assuming jQuery) - sending yor myEdits object Javascript部分(假设jQuery)-发送yor myEdits对象

$.ajax({
  type: "POST",
  url: 'Home/MyMethod',
  data: myEdits,
  success: success,
  dataType: dataType
});

HomeController HomeController的

public void MyMethod(myEdits edits)
{
    return View("/ViewDetails/EditConfirmation/", edits);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理在 Python 中字符串化的 json 对象 - How to handle json object that was stringified in Python Javascript,对字符串化的JSON对象中的键/值的数量进行计数 - Javascript, count number of key/values inside stringified JSON Object javaScript:使用JSON.parse解析字符串化对象会删除引用 - javaScript: Parsing a stringified object with JSON.parse removes references 向控制器发送JSON字符串化javascript对象时发生POST 500错误 - POST 500 error when sending JSON stringified javascript object to controller 如何使用Javascript XMLHttpRequest将JSON数据对象发送到服务器? - How to send a JSON data object to the server using Javascript XMLHttpRequest? 如何将字符串化的Surveyjs类JSON对象转换为实际的JSON - How to convert stringified surveyjs JSON-like object into actual JSON 使用JavaScript函数将Json对象发送到服务器 - Send a Json object to server with javascript function javascript indexOf与JSON字符串化对象 - javascript indexOf with JSON stringified objects 如何在存储在 Localstorage 中的字符串化 JSON 中仅删除一个 object? - How to delete only one object within a stringified JSON stored in Localstorage? 如何将字符串化对象中的双引号转义以将其解析为有效的 JSON? - How to escape double quotes in stringified object to PARSE IT into valid JSON?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM