简体   繁体   中英

c# http post get data from Request.Form in readable string format

i have this on client side

            $http({
                method: 'POST',
                url: "/FileManager/SavePivotFile",
                data: JSON.stringify(tempOptions),
                params: {
                    fileName: $stateParams.report.FileName
                },
                headers: { 'Content-Type': "application/x-www-form-urlencoded" },
            })
            .then(function (success) {
                showNotification('The changes have been saved.', 'info');
            }, function (error) {
                showNotification('Failed to save the file.', 'error');
            });

And this on server

    [System.Web.Http.HttpPost]
    public void SavePivotFile(string fileName)
    {
        var qqq = Request.Form;

    }

When i send the data in server i get the string like "{%7b%22dataSource%22%3a%7b%22type%22%3a%22xmla%22%2c%22c"

How can format this to human readable format like: "{"dataSource":{"type":"xmla","columns":[{"name"

So later can write this to a file?

you can just use UrlDecode

var str=   HttpUtility.UrlDecode("%7b%22dataSource%22%3a%7b%22type%22%3a%22xmla%22%2c%22c");

But I think that your Json Doc is not complete

Note You have to reference System.Web if It is not done

You can avoid encode/decode. On client side:

var somedata = { prop1: 1, prop2: 2 };
$http({
            method: 'POST',
            url: '@Url.Action("Send", "Test")',
            data: { Content: JSON.stringify(somedata) }
        });

On server:

public class TestController : Controller
{
    [HttpPost]
    public string Send(Data data)
    {
        return data.Content;
    }
}

public class Data
{
    public string Content { get; set; }
}

In this case you will get normal json in data.Content property.

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