C
C
Chamyys2018-08-21 16:02:12
JavaScript
Chamyys, 2018-08-21 16:02:12

How to pass json file to controller via AJAX correctly?

Hello! There was a need to transfer a json file from the client to the server using an AJAX Post request. Everything is initialized, but I get error #500. Thank you all in advance!

//javascript клинт
     var visualControl = {
            WaferName: NumberOfBar,//string
            surname: Surname,//string
            dateTime: Date(),//Date
            listOfDefects : masOfDefects,//Array

        }



        $.ajax({

            type: 'POST',
            url: 'http://localhost:53114/AddDefect/ReturnDefect',
            data: JSON.stringify(visualControl),
            success: function(){
                alert("Данные отправлены, Спасибо, " + SurnameCoded + "!");
            },
            dataType: 'json'

        });

    }

//контроллер
   [HttpPost]
        public void  ReturnDefect (dynamic json)
        {
            

            StreamWriter sw = new StreamWriter("C:\\kek/kek.txt");
            sw.WriteLine(json.WaferName);
            sw.WriteLine(json.surname);
            sw.WriteLine(json.dateTime);
            sw.WriteLine(json.listOfDefects);
           


            //sw.Close();

       

        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2018-08-21
@Chamyys

1) Mark the input parameters with the [FromBody] attribute. What you wrote is theoretically a dynamic parameter, which should consist of 80-90 fields, which are actually contained in the request.
2) Write a model class into which the query mapping should actually be done.
PS your request is clearly directed to localhost:53114. If your application is in the same domain as the client part, remove this link, and if not, then make sure that CORS is correctly configured in your application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question