Invoking WebMethods With XmlHttpRequest And Pure JavaScript?
Jun 22, 2010
I have what should be a relatively simple task that's frankly got me stumped. I've researched it until my brain is fried, and now I'm puntingHere's the scenario:I have an ASPX page (Q2.aspx) that is decorated with the WebService,WebServiceBinding, and ScriptService attributes. That page contains a method, GetAllContacts, that is decorated with the WebMethodattribute and returns a string containing JSON data. (For what it's worth, the pageitself contains no other controls or functionality.)I have an HTML page that contains JavaScript which uses the XmlHttpRequestobject to invoke the GetAllContacts WebMethod on the ASPX page and transformthe JSON data into an HTML table. I have verified that my Web.Config file contains the appropriate protocol handlersfor HttpGet and HttpPut in the WebServices section under System.Web.webServices.
I have verified that my Web.Config file contains the ScriptModule entry under theSystem.webServer.modules section, and that it matches the appropriate documentation.
However, when I view the HTML page in a browser, the following occur:The web request goes through, but the results are for the unprocessed HTML from the ASPX page.The GetAllContacts method is never invoked, as evidenced by setting a breakpoint in its code.The code to invoke the Web service, however, is invoked, and the JavaScript callbackfunction that is invoked upon request completion is properly invoked.
It appears that the JavaScript code is largely set up correctly, but for some reason that is completely escaping me at this point, the HTML page will simply not execute the WebMethod on the ASPX page, and simply returns the page as though it were a plain HTML GET request. Clearly, an HTML document can't be evaluated by JavaScript's eval function, which brings me to my problem. (Also note that the JSON data appears nowhere in the HTML that's returned.)I am, frankly, baffled. I've looked at dozens of Microsoft articles, StackOverflow posts, CodeProject articles, and who knows what else. My code looks like it's okay. But I know better. I'm missing something simple, stupid, and obvious. I just need someone to point it out to me.Below you'll find the ASPX page code and the HTML code, in the hope that they'll shed some light.ASPX Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Q2.aspx.cs" Inherits="Satuit.Q2" enablesessionstate="False" %>
<html>
<body>
[code]...
View 1 Replies
Similar Messages:
Jan 25, 2011
I am using PageMethods in asp.net. In code behind i have written a method which should call a javascript function for each loop & should come back & continue the loop.Is it possible?
[WebMethod]
public static void GetStatus()
{
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(500);
//Call javascript function
}
}
View 2 Replies
Jan 12, 2011
I have many AJAX forms on the page and on button click I need to submit them all. Regular **forms.each(function (index, form) { $(form).submit();} won't work for me because in this way ONLY the last form will be submitted eventually. Therefore, I need submit them via $.ajax(...). But I want to enable submit ONLY and ONLY if form is VALID On "submitForms" click the fucntion "submitTest" is invoked.
From jQuery documantation "submitHandler" would be called if form is VALID. So, the validation works and "submitHandler" is NOT invoked if form is INVALID. BUT, the XmlHttpRequest is sent anyway, though "submitHandler" isn't invoked.
<% using (Ajax.BeginForm("FormPost", "Customer", null, new AjaxOptions() { HttpMethod = "POST" }, new { @class = "form-container" }))
{ %>
<%: Html.TextAreaFor(m => m.Name, new { width = 440, height = 100 })%>
<input type="button" value="submitForms" />
<% }%>
<script type="text/javascript">
function submitTest(){
var forms = $(".form-container");
forms.each(function (index, form) {
$(form).validate({
submitHandler: function (form) {
$.ajax(
{
....
});
}
});
});
forms.each(function (index, form) {
$(form).submit();
});
}
</script>
View 1 Replies
Dec 2, 2010
In your opinion, what's the best way to create the server side to a pure Javascript application with ASP.NET? WCF rendering JSON? IHttpHandler? Update Like GMail, that runs in the browser (with a lot of Javascript) and submit and receive data with Ajax, for example.
View 2 Replies
Dec 3, 2010
The intent is to create a set of web services that people can reuse. These services mostly interact with a backend DB creating, retreiving and processing data. We want to expose services so that people can use to create data mashups and other applications.End users are webpages that can be within our domain or outside our domain. For pages outside the domain we plan to release widgets that would be configured to retreive and display the data.
One requirement - application should be extremely scalable in terms of the number of users it can handle.Our code base is .net and we are looking at ASPX webmethods (or ASHX), ASMX webmethods and WCF (starting to read up on WCF). In terms of security/access I found that maintaining sessionid, memberships is doable in all three. WCF seems a bit complicated to setup. I could not immediately see the value of asmx when we can get all done just using a webmethod in aspx (with a little tweaking). Also, assuming that with the ASP.NET MVC2 I might be able to get clean urls as well for these webmethods.QuestionsWhich one will be the most effective in terms of performance and scalability? Any reason why I should choose WCF or ASMX?
EDIT I kind of understand that WCF is the way to go. Just to understand the evolution of the technologies it would be good if someone can throw light on why a aspx webmethod is different from an asmx when similar things (apart from discovery) can be accomplished by both. The aspx webmethods can be made to return data in other formats (plaintext, json). Also, it seems that we can build restful services using ashx.
View 2 Replies
Feb 18, 2011
I have the following setup, where I am custom validating a textbox. It works fine as long as I am manually typing in the textbox and changing the text and focusing out of the textbox.
[Code]....
when I try to invoke validation change event from javascript (using JQuery 1.4.2)
function copyCity() {
$('#<%= tbpCity.ClientID%>').value = "Some City";
$('#<%= tbpCity.ClientID%>').trigger("change");
}
the custom validation is not being invoked.
How can I inovke customvalidator to do validation?
note: I have verified this works on FireFox but not on IE. how to fire change event in IE.
View 1 Replies
Jan 29, 2010
I have an MVC 1.0 app that makes several AJAX calls from JS files. Originally I was just specifying the relative URL like so:
[Code]....
This works but it feels rather like a hack. Is there any way to do what Url.Action does but in the JS code itself? I've had a bit of a search but I'm having trouble coming up with the correct keywords, or else the info just isn't out there.
View 4 Replies
Aug 11, 2010
I got XYZ page which got few divs inside of himin 1 of the divs there in an updatepaneland in another there are buttons which being inserted into the page using javascriptWhat I want to do is once a button is clicked, it would invoke a method using javascript in the server which would update the updatepanel
View 2 Replies
Jun 23, 2010
I have function established in child page, that is using in the childpages. I have to invoke the child page function from the master page. How it is possible in javascript
View 2 Replies
Aug 2, 2010
I want to return pure JSON from an MVC 2 Action. How can i do this?
View 4 Replies
Dec 5, 2010
calling a SAP Webmethod using ASP.NET application.
View 1 Replies
Mar 9, 2010
I want to alternate rowcolor in a GridView, using pure CSS, ie: I don't want to use asp.net themes; I'd rather not have to use jQuery, or define define AlternatingRowStyle-BackColor on each gridview (unless there's a reason I must). Here's my CSS (that isn't working):
.gridView {font-size:11px
}
.gridView tr:nth-child(even) {background-color: #FFF}
.gridView tr:nth-child(odd) {background-color: #FFCC00}
.gridView tr:nth-child(even) td {background-color: #FFF}
.gridView tr:nth-child(odd) td {background-color: #FFCC00}
(I included the .gridView {font-size:11px} just to confirm I am using the proper CssClass.) Is this not possible, or am I doing something wrong.
View 1 Replies
Apr 20, 2010
give a link with sample if possible
View 4 Replies
May 6, 2010
I want an asmx webservice with a method GetPeople() that returns the following XML (NOT a SOAP response):
<People>
<Person>
<FirstName>Sara</FirstName>
<LastName>Smith</LastName>
</Person>
<Person>
<FirstName>Bill</FirstName>
<LastName>Wilson</LastName>
</Person>
</People>
View 4 Replies
Dec 28, 2010
[Code]....
i need to call pure JS inside UpdatePanel becouse of DayPilotPro but when i am using UpdatePanel this is not possible. How can i call it?
View 1 Replies
May 15, 2010
I want to reduce postback in one of my application page and use ajax instead. I used the WebMethod to do so.. I have a static WebMethod that needs to access the session variables and modify. and on the client side, i am calling this method using jQuery. I tried accessing the session as follows:
[WebMethod]
public static void TestWebMethod()
{
[code]...
The values are displayed correctly and seems to work.. but i would like to know if this practice is allowed as the method is a static methods and would like to know how it will behave if multiple people access the application.I would also like to know how developers do these kind of tasks in ASP if this is not the right method.
View 1 Replies
Mar 2, 2010
Value of window.XMLHttpRequest is Undefined when i run my application even in IE7 or IE8, Is there anything i have to enable in IE7 to make it work.
View 3 Replies
Nov 22, 2010
I have a webservice (using asp.net VS 2008).If I have two webmethods with same name but different input parameters and same output parameters. Just like given below. Can't I have two webmethods with same name with different parameters?
Code:
[WebMethod]
public Portfolio GenerateData(string As_of_Date, bool PortfolioLoadfromArchive)
{
Portfolio aPortfolio = null;
[code]...
Both TestProj.WebServices.Portfolio GenerateData(System.String) and TestProj.WebServices.Portfolio GenerateData(System.String, Boolean) use the message name 'GenerateData'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.
View 10 Replies
Apr 24, 2010
I have a cascading dropdown (3 of them) Type, Categories and Sub Categories. Type loads first and upon selection of Type, Category load and selection of Category, Sub Category loads. Also i have 2 buttons, "Add Category" and "Add Sub Category" Upon clicking on these buttons, i call a JQuery Modal Form to add them. I use Webmethod in code behind to add them to database
This works perfectly in ASPX page.
Since I need use this in 3-4 pages, i thought of making the above as User control (ASCX). When i try to use this in a webpage, the webmethods in ASCX don't get called.
View 2 Replies
Feb 17, 2011
I'm attempting to create a pure t-sql representation of the default SHA-1 password hashing in the ASP.Net Membership system. Ideally, what I would get would be this:
UserName Password GeneratedPassword
cbehrens 34098kw4D+FKJ== 34098kw4D+FKJ==
Note: that's bogus base-64 text there. I've got base64_encode and decode functions that round-trip correctly. Here's my attempt, which doesn't work: SELECT UserName, Password, dbo.base64_encode(HASHBYTES('SHA1', dbo.base64_decode(PasswordSalt) + 'test')) As TestPassword FROM aspnet_Users U JOIN aspnet_membership M ON U.UserID = M.UserID
I've tried a number of variations on the theme, to no avail. I need to do this in pure T-Sql; involving a console app or something like that will double the work. So if anyone can supply what precisely the syntax should be to duplicate that password from the ASP.Net membership stuff
View 2 Replies
Aug 4, 2010
Analysis and design C# application with pure object orineted feature .
for example
I get all the requirement for the user, How to analysis the system , keeping future changes in mind , design and system with pure object oriented feature .
in Short : design system with pure object oriented feature so that it flexible so that in future new version can be launch easily
View 1 Replies
Oct 12, 2010
I am trying to use jQuery .load() to get straight html from a asmx web service:
$('#target').load('MyService.asmx/GetHtml');
In .NET code, GetHtml() returns as string:
[WebMethod(EnableSession = false)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Xml)]
public string GetHtml()
{
return "<span>Hi</span>";
}
That returns:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/"><span>Hi</span></string>
Notice that the string is encoded. With that encoding, $.load doesn't work right. The displayed text actually has the tags shown.
How can I get the WebMethod call to return just this?
<span>Hi</span>
View 4 Replies
Feb 22, 2011
problem in requesting response from an aspx url in xmlhttprequest...
xmlhttp.open("GET","a.aspx",true);
View 2 Replies
Mar 23, 2010
I want to use Ajax XMLHttpRequest and get data from a database table and show it on the screen. I followed the link "Guide to Using AJAX and XMLHttpRequest from WebPasties" http://www.webpasties.com/xmlHttpRequest/index.html but there the code some part is written in PHP. I want entire steps and code in asp.net etc. Can you please give me a link where exactly I create XMLHttpRequest and get data from database. To achieve the above requirement do I need to AjaxFy my asp.net webapplication by adding configuration in the web.config file.
View 5 Replies
Jan 12, 2011
On "submitForms" click the fucntion "submitTest" is invoked. From jQuery documantation "submitHandler" would be called if form is VALID. So, the validation works and "submitHandler" is NOT invoked if form is INVALID. BUT, the XmlHttpRequest is sent anyway, though "submitHandler" isn't invoked.
<% using (Ajax.BeginForm("FormPost", "Customer", null, new AjaxOptions() { HttpMethod = "POST" }, new { @class = "form-container" })) { %> <%: Html.TextAreaFor(m => m.Name, new { width = 440, height = 100 })%> <input type="button" value="submitForms" /> <% }%><script type="text/javascript">function submitTest(){var forms = $(".form-container");forms.each(function (index, form) { $(form).validate({ submitHandler: function (form) { $.ajax( { .... }); } });});forms.each(function (index, form) { $(form).submit();});}</script>
View 4 Replies