简体   繁体   中英

How to post data from C# to an ajax website?

I work in C# and so far I used WebRequest method to GET and POST data. I use Fiddler to check what is the browser doing and I got to a point where the data is retrieved from Ajax after posting some data. I am not sure if I have to add to my project a javascript page or what and what code do I need in the javascript file and how to call it. In essence, I have to post the data {"name":"ABCD"} to url www.example.com/Website.AJAX,Website.ashx.

Ajax is not so different from ordinary request, so you can just post it as usual. Most likely problem is how the backend treat that it is an ajax request (if it does at all).

As it looks like you are using the WebForms there on backend, you just need to add a special header most likely ( X-Requested-With ). Some frameworks add it, though it's not a real requirement of the ajax request.

All in all I would just post an ordinary request with WebRequest as you did before. If that does not work, you need to study the original request from web UI to see what is different. Eg a special header or request Content-Type is JSON or something like that.

PS If you use JSON in the body it's better to explicitly set content type to application/json; charset=utf-8 application/json; charset=utf-8 unless there is something special with the server.

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