I use the guidelines outlined here ( http://www.odetocode.com/articles/473.aspx ) to write an AJAX JavaScript web system using simulated Namespacing and prototyping.
In one of my prototype methods, I call the $ method . ajax in jQuery . What I then want to do is pass the returned JSON data to a method inside my webchat JavaScript namespace.
The problem is that I created an instance of my JavaScript web chat, I cannot directly call the method inside it, because I need to access it through the instance.
The key part in the code below is
success: function(data, textStatus) {
this.GetUpdate_Success(data)
},
I think because we are inside the $ .ajax () method, which this one no longer applies to our WebchatV3 object.
The full JavaScript code is below:
var AvonAndSomerset = {}
AvonAndSomerset.WebchatV3 = function(memberId, passcode) {
this.Members = new Array();
this.Questions = new Array();
this.currentMember = new AvonAndSomerset.WebchatV3.Member(memberId, passcode, null, null, null, null, null);
$.ajaxSetup({ type: "POST", contentType: "application/json; charset=utf-8", dataType: "json" });
}
AvonAndSomerset.WebchatV3.prototype =
{
GetUpdate: function(StartUp) {
$.ajax({ url: "JSON.aspx/Members_GetChanges",
data: "{ MemberID: " + this.currentMember.memberId + ", Passcode: \"" + this.currentMember.passcode + "\", ReturnAll: " + StartUp + " }",
success: function(data, textStatus) {
this.GetUpdate_Success(data)
},
error: function(result) {
alert('Members_GetChanges() failed: ' + result.responseText);
}
});
},
GetUpdate_Success: function(data) {
alert('The AJAX call was successful!');
},
Members_DoesExist: function(MemberID) {
alert('Searching for ' + MemberID);
alert(this.Members.length);
}