3

I posted an XmlRequest with native JavaScript to my controller, but it doesn't accept the request body as a parameter. I.e. it reads it as a null even if the parameter is meant to be a string.

The request:

request.open("POST", "https://localhost:44328/CommodityTypes/PostData");
//request.setRequestHeader('Content-type', 'text'); // maybe that makes the problem?
request.send("Message");

The controller:

[HttpPost]       
public string PostData(string Text)
{
  return JsonSerializer.Serialize(Text);
}

Will be happy to get any advice on it.


After getting deeper into the business I found, that one can specify 'object' as a parameter to parse for ('[FromBody]' attribute stays), getting a curious object, that gives the JSON message invoking ToString() method. If you have a variable or uneven structure of incoming JSON you may use this aproach.

Though there must be some other, meant way of handling the issue.

  • 1
    You need to tell it the name of your parameter, I think. Try `request.send("Text=Message");`. Otherwise you just send a nameless value, so the server cannot match it to the "Text" parameter in the action method. See also https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest – ADyson May 04 '20 at 20:47
  • 1
    The `string Text` parameter will, by default, look for a field in your `Post` collection called `Text` (or `text`; it's case insensitive) to bind to. If it can't find that, it will return `null`. You don't appear to be passing any fields along with your request. – Jeremy Caney May 04 '20 at 20:47
  • 1
    Is this a web-api controller or an mvc controller? – Igor May 04 '20 at 20:50
  • Jeremy, if you meant, that I should pass parameters in the request line, it would rather work, but it isn't my initial idea. If you meant, that my parameter named "Text" in controller can be taken from body { "Text": "YourText" } stringified automatically - it isn't. But declaring parameter to be some JSON class or Dictionary works. – IgorZhurbenko May 05 '20 at 11:16

2 Answers2

3

You can create object data = { "text": "YourText" } and send JSON.stringify(data)

And need set header xmlhttp.setRequestHeader('Content-Type', 'application/json');

var uri = '/CommodityTypes/PostData';
var xmlhttp;
xmlhttp = new XMLHttpRequest();

var data = { "text": "YourText"  };

var sendstr = JSON.stringify(data);

xmlhttp.open("POST", uri, true);
xmlhttp.setRequestHeader('Content-Type', 'application/json');
xmlhttp.send(sendstr);

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        alert(xmlhttp.responseText);
}
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • Thank you, sir. It worked just as I wanted initially. Important thing is to specify [FromBody] instruction along with parameter in the related controller, so it knows where to take the value from. And also [HttpPost] above the method. – IgorZhurbenko May 05 '20 at 10:45
2

in POST you have to send the Text parameter as FormData so it can be recognized and mapped by the method

var data = new FormData();
data.append("Text", "test");

var xhr = new XMLHttpRequest(); 

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://localhost:44328/CommodityTypes/PostData");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.send(data);
  • if you take the time to explain your answer you are more likely to get upvotes and acceptance by the OP. If you just dump some code, people have to figure out by themselves what you changed, and, more importantly - _why_ you changed it. Please write something to show why your code solved the problem. – ADyson May 04 '20 at 21:29
  • Thanks, I've tried using forms, but they are unhandy, because they redirect by default, which I badly want to avoid, as well as rewriting the default procedure. – IgorZhurbenko May 05 '20 at 10:57