I wanted to use a web service published with ASP.NET AJAX from
Prototype Version 1.5.1.1
It''s easier to call an ASP.NET AJAX web service by using an ASP.NET
AJAX page with a proxy class, but I have an application which still
has many Classic ASP pages and using Prototype seemed like a better
idea than hand coding the the interactions, or figuring out how to
include all the right ASP.NET AJAX libraries and use a proxy class.
ASP.NET AJAX expects to be sent JSON and the return JSON, but not in
the exact same manner that Prototype seems to expects it. I made some
minor adjustments to Prototype to get it to work.
Here is some sample code that calls the service:
new Ajax.Request(''User.asmx/GetUserId'', {
method:''post'',
contentType: ''application/json'',
postBody : {lsUserName : $(''uoManagerUsername'').value,
lsCatalogName : $(''uoMainCatalog'').value},
onSuccess: function(transport, json){
var lnId = eval(json);
if ($(''uoManagerUsername'').value ==
''none'') lnId=0;
if (lnId >= 0) {
$(''uoManagerUsernameText'').innerHTML = $
(''uoManagerUsername'').value;
$(''uoManagerUsername'').value = '''';
$(''uoManagerId'').value = lnId;
} else alert(''"'' +
$(''uoManagerUsername'').value + ''" was not
found.'');
$(''uoChangeManagerButton'').disabled = false;
}
});
I started messing with the "parameters" first, but switched to using
"postBody" because it required less code changes to Prototype.
These are my Prototype changes:
Replace this at line 1047:
this.body = this.method == ''post'' ?
(this.options.postBody ||
params) : null;
this.transport.send(this.body);
With this:
if (this.options.contentType.indexOf(''json'') >= 0) {
this.transport.send(Hash.toJSON(this.options.postBody));
} else {
this.body = this.method == ''post'' ?
(this.options.postBody ||
params) : null;
this.transport.send(this.body);
}
Replace this at line 1145:
evalJSON: function() {
try {
var json = this.getHeader(''X-JSON'');
return json ? json.evalJSON() : null;
} catch (e) { return null }
},
With this:
evalJSON: function() {
try {
var json = this.getHeader(''X-JSON'');
var contentType = this.getHeader(''Content-type'');
if (!json && contentType.indexOf(''json'')>=0)
json this.transport.responseText;
return json ? json.evalJSON() : null;
} catch (e) { return null }
},
I don''t think these changes broke anything that it already did, and it
now works to send JSON and received JSON from an ASP.NET AJAX web
service.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---