Actually request.

var xml = "example"; function send(xml) { //отправка данных на сервер $.ajax({ type: 'POST', url: '/Home/Form', data: xml, success: function (responce) { SaveToLocal("s",xml);// префикс s - sended notification("Данные успешно отправлены", "success"); }, error: function (xhr, str) { SaveToLocal("u",xml);// префикс u - unsended notification("Подключение недоступно, данные сохранены в локальном хранилище","warning"); setTimeout(DelayResend, 30 * 1000);// функция отложенной отпраки с тайм аутом 30 сек. } }) }; 

Controller:

 [HttpPost] public void Form(string xml) { string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); using (StreamWriter outputFile = new StreamWriter(mydocpath + @"\WriteLines.txt")) outputFile.WriteLine(xml); // return View(); } 

Form must write a string to a file. The file is being created, but it is empty. During the transfer, the "success" block is executed - it means there are no transmission errors. But where is the string lost?

    2 answers 2

    Good day! you do not have the following parameters in the data parameter:

     data: { xml: xml }, 

    it will turn out:

      var xml = "example"; function send(xml) { //отправка данных на сервер $.ajax({ type: 'POST', url: '/Home/Form', data: { xml: xml }, success: function (responce) { SaveToLocal("s",xml);// префикс s - sended notification("Данные успешно отправлены", "success"); }, error: function (xhr, str) { SaveToLocal("u",xml);// префикс u - unsended notification("Подключение недоступно, данные сохранены в локальном хранилище","warning"); setTimeout(DelayResend, 30 * 1000);// функция отложенной отпраки с тайм аутом 30 сек. } }) }; 

      Create an object with the "xml" property on the side of the script and pass it. And on the controller side, take a class with the "xml" property. This is most likely due to the fact that you are trying to pass the post with a request for the value type.