Based on your description and code provided, I see that you use the XMLHttpRequest to call the aspx page method.
As we all know that "ASP.NET page methods (or web services for consumption in JS) uses JSON serialization for i/p and o/p.
So you need to set JSON as the content type for the request and actually send the JSON string in the body i.e."
# If you want to call the aspx page method, you should set the Content-Type as "application/json" like below.
function UploadFile() {
var sendStr = "{name:'dddd'}" ;
var uploadServerSideScriptPath = "XmlHttpRequestUploadFile.aspx/test";
var xhr = new XMLHttpRequest();
xhr.open("POST", uploadServerSideScriptPath, false);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(sendStr);
}
None
0 Points
4 Posts
Call c# server-side function using xmlhttprequest
Dec 22, 2013 07:05 AM|InbalT|LINK
I try to upload a file using xmlhttprequest.
Here is my client-side code:
I post to
test function on server side is:
but I never get to this function. I put a break point but it never being hit.
what am I doing wrong?
xmlhttprequest C asp.net upload
All-Star
30411 Points
3628 Posts
Re: Call c# server-side function using xmlhttprequest
Dec 22, 2013 10:14 PM|Fuxiang Zhang - MSFT|LINK
Hi InbalT,
Thank you post the issue to asp.net forum.
Based on your description and code provided, I see that you use the XMLHttpRequest to call the aspx page method.
As we all know that "ASP.NET page methods (or web services for consumption in JS) uses JSON serialization for i/p and o/p.
So you need to set JSON as the content type for the request and actually send the JSON string in the body i.e."
# If you want to call the aspx page method, you should set the Content-Type as "application/json" like below.
Page method:
# If you want to upload the file, please try to use the page load method of aspx. Just Like below.
Aspx:
Code Behind:
Thanks.
Best Regards!
xmlhttprequest C asp.net upload
None
0 Points
4 Posts
Re: Call c# server-side function using xmlhttprequest
Dec 23, 2013 02:07 AM|InbalT|LINK
Thank you vaery much for the help!
Instead of using Page Load, I created my own .axd handler that runs ProcessRequest.
Is it sound like a good solution?
xmlhttprequest C asp.net upload
All-Star
30411 Points
3628 Posts
Re: Call c# server-side function using xmlhttprequest
Dec 23, 2013 08:26 PM|Fuxiang Zhang - MSFT|LINK
Hello,
Yes, it is a nice solution, that will make your application clear and simple.
Generally, axd handlers are used to download resources embedded in an assembly. so it is mostly used by control developers.
You can also try to use the ashx handlers to complete it. If you never mind, please share the solution with simple code and that will help others.
Thanks.
Best Regards!
xmlhttprequest C asp.net upload