I tried to do an AjaxCall and want to pass two or maybe more arguments. One of the arguments is an Xml as String and another is just a name as string. I figured out how to do pass the xml file.
function sendXml(documentIdentifier) {
var xmlAsString = getXml();
$.ajax({
type: "POST",
url: "MyPage.aspx",
data: xmlString,
contentType: "text/xml",
success: function (msg) {alert('Succeed')},
error: function (msg) { alert('Failed') }
});
}
Additionaly i want to return a single string from my code behind.
protected void Page_Load(object sender, EventArgs e)
{
Console.WriteLine();
Stream s = (Request.InputStream as Stream);
TextReader tReader = new StreamReader(s);
string strXml = tReader.ReadToEnd();
//do Stuff Response.Write(myString);
Response.End();
}
How can I add other parameters to the ajax-call and how can I access them from code behind?
How can I access the Response-string from the ajax-success-method)?
Hi AidyF,
Thanks for your response. I tried the first one in a single example and everything worked quite good but when i was implementing the exactly same example in my web project it leaded to a wrong behavior:
The WebMethod is not called, at all.
Instead of the WebMethod the Page_Load method gets called (IsPostBack is FALSE!!!)
Consequently the return value is not a single string, its the complete Page.
Has anybody an idea where this wrong behavior might come from?
In my webproject im using additional controls (ASP. NET AJAX from Telerik), maybe i'm using something wrong or anything is missing.
Hi Raigad,
I wanna pass two strings, one of them is a complete xml file as string and the other is just a single short string like you said. With the line
data: xmlString,
i add the xmlString argument but how can i add additional arguments, to this?
I'd like to know the syntax and the way how i can use parameters and how i can acces them from code behind.
2nd i'd like to know if in that way it is possible to give return arguments. My first approch was just manipulate the hole response in the Page_Load method and write the string in it, instead of the html i get the string i wanted and can use this now in js).
ironhaert
0 Points
8 Posts
How do i pass Arguments in the AjaxCall
May 16, 2012 08:31 AM|LINK
Hi everybody,
I tried to do an AjaxCall and want to pass two or maybe more arguments. One of the arguments is an Xml as String and another is just a name as string. I figured out how to do pass the xml file.
function sendXml(documentIdentifier) { var xmlAsString = getXml(); $.ajax({ type: "POST", url: "MyPage.aspx", data: xmlString, contentType: "text/xml", success: function (msg) {alert('Succeed')}, error: function (msg) { alert('Failed') } }); }Additionaly i want to return a single string from my code behind.
protected void Page_Load(object sender, EventArgs e) { Console.WriteLine(); Stream s = (Request.InputStream as Stream); TextReader tReader = new StreamReader(s); string strXml = tReader.ReadToEnd(); //do StuffResponse.Write(myString); Response.End(); }
Greetings,
ironhaert
AidyF
Star
9204 Points
1570 Posts
Re: How do i pass Arguments in the AjaxCall
May 16, 2012 08:53 AM|LINK
You need to use a WebMethod on your page, then you can use Mircosoft's PageMethods wrapper to call it
http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods-in-aspx-pages.aspx
Or jQuery (this is with a WebMethod attached to a WebService -> JUst richt-click your project and Add->New Item->(Web) Web Service)
http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/
Raigad
Contributor
5129 Points
956 Posts
Re: How do i pass Arguments in the AjaxCall
May 16, 2012 09:12 AM|LINK
Save some data to the server and notify the user once its complete.
jQuery Code
$.ajax({ type: "POST", url: "some.aspx", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } });Mark as Answer, if the post helped you...
Visit My Blog
ironhaert
0 Points
8 Posts
Re: How do i pass Arguments in the AjaxCall
May 16, 2012 12:14 PM|LINK
Hi AidyF,
Thanks for your response. I tried the first one in a single example and everything worked quite good but when i was implementing the exactly same example in my web project it leaded to a wrong behavior:
Has anybody an idea where this wrong behavior might come from?
In my webproject im using additional controls (ASP. NET AJAX from Telerik), maybe i'm using something wrong or anything is missing.
Greetings,
ironhaert
ironhaert
0 Points
8 Posts
Re: How do i pass Arguments in the AjaxCall
May 16, 2012 12:29 PM|LINK
Hi Raigad,
I wanna pass two strings, one of them is a complete xml file as string and the other is just a single short string like you said. With the line
i add the xmlString argument but how can i add additional arguments, to this?
data: {name: 'xmlName', data: xmlString} ???
data: xmlString, name="testName" ????
I'd like to know the syntax and the way how i can use parameters and how i can acces them from code behind.
2nd i'd like to know if in that way it is possible to give return arguments. My first approch was just manipulate the hole response in the Page_Load method and write the string in it, instead of the html i get the string i wanted and can use this now in js).
Greetings,
ironhaert
ironhaert
0 Points
8 Posts
Re: How do i pass Arguments in the AjaxCall
May 16, 2012 02:39 PM|LINK
BU XI - MSFT
All-Star
22367 Points
2704 Posts
Microsoft
Re: How do i pass Arguments in the AjaxCall
May 18, 2012 06:27 AM|LINK
Hello
In ASP.NET, for aspx handler, you can have the page the render your return string in the Render event. Refer to this post please, http://forums.asp.net/p/1759500/4788707.aspx/1?Re+XMLHttpRequest+question
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
ironhaert
0 Points
8 Posts
Re: How do i pass Arguments in the AjaxCall
May 23, 2012 12:24 PM|LINK
Hi BU XI - MSFT,
I got it work:
Response.Write(myString); Response.End();function openPdfPreviewId() { var xmlString = getXml(); $.ajax({ type: "POST", url: "Test.aspx?Param1=ParaValue1", data: xmlString, contentType: "text/xml", success: successAjax, //complete: completeAjax, error: function (msg) { alert('Failed') } }); } function successAjax(myString) { alert.open(myString); }Greetings,
ironhaert