Skip to main content

How to call a web method by JavaScript

I didn’t get a time to write something here for a long time. Yes I can say I was busy. (But I was not :)) Anyway I thought to write about something finally and I promise (this is a serious promise) that I will try to do something (do a posting actually) on my blog more frequently.
The ability to call the web methods by JavaScript on .net is very very useful and we can implement the web pages more efficiently by using this. For example we do not need to send whole page and update. Yes this is AJAX (that what I think).
Here is a web method example. It should be on the aspx page code behind and should be a static (shared) method. I have tried to insert the web methods on user controls and was unable to do so (if you know a way please share with us). This is on
 Public Shared Function GetMyData(ByVal studentID As Integer, ByVal schoolID As Integer) As Student
        Dim studentService As New StudentService()
        Dim result As Student = studentService.GetStudent(studentID, schoolID)
        Return result
    End Function

GetMyData method will create a Student entity based on the IDs passed in and this method can even call to DB to get the data (since this is on server side we can do anything).
Then this method (GetMyData) needs to be executed by JavaScript. Here is the example on how to do that.
this.GetStudent = function(args)
        PageMethods.GetMyData(_studentID, _ schoolID, _this.UpdateStudentFields, _this.ServiceError);

In here if you have noticed I have send 2 more parameters (_this.UpdateStudentFields, _this.ServiceError) than the web method accepts. (Was not by any mistake:))
_this.UpdateStudentFields – is the JavaScript method name that will execute when we returned from the web method. So basically what happens is Student entity will be passed into this method (UpdateStudentFields) as the result. So then we can do anything by JavaScript by using this result entity.
this. UpdateStudentFields = function(result)
        if (result.Succeeded)
        //assign the values to the fields
        //studentname = result.Name;

_this.ServiceError – this is the javascript method name that executes if there were any exceptions. So then we can do anything even with the error (exception actually). I prefer to show the errors to the end user (swallowing exeptions is not a good practice).
this.ServiceError = function(error)
       //this will show the error on page
// AddError method should be implimented to do so … 

Hope this was helpful and if there is any issues or if you know a better way to impliment this please leave a comment.


Post a Comment

Popular posts from this blog

Google API v3 with PHP using Blogger service

It was really hard for me to understand how the Google APIs are working at the first point and took few days for me to figure out. But after a successful working prototype it seems very easy. And also when I am searching for a simple example I was unable to find a good one that I can understand.

So let me list down step by step what I have done with URLs and as simple as I can.

Create a Google applocation - on the "Blogger API v3"Get the latest APIs client library for PHPlocation - the files to your host location on on localhostExtract the files to folder  named "GoogleClientApi"Create your php file outside of the folder Copy paste following code into the file and do the changes as needed  By changing the scope and the service object you can access all the services that is given by Google APIs through the PHP API library set given.
If there is anything …

How to get indexed by Google & Yahoo

I have seen a lot of ads and services online that provide the facility to get higher page ranks on search engines. But when I asked about those services on Google their respond was not much positive because they said it can decrease you page rank (because those services cannot be trusted always). It is happening because the page rank is depending on page visitors (as Google say). So the better way is to let the Google to index your site by sending them the address of your website. : here you can improve the traffic with the webmaster tools and you can send your website contents also. It is a legal, simple and direct hit.
You can send your web site address (even the blog) to Yahoo: also. It helps them to index your pages on their search engines.

It is better to come up with the content and services which are really useful for the visitors and the traffic will be increased regularly. I have gone through a lot of expe…

How to Install ColdFusion 8 on Linux?

It was real pain for me to install ColdFusion on my Linux box with Apache. Anyway we have installed it and I thought to share it because this may help you also to install ColdFusion on a Linux box.
First of all open a shell and then continue on this.

Grant Execute permission to the Coldfusion‐8‐lin.bin binary file
chmod +x Coldfusion‐8‐lin.bin

Execute Coldfusion‐8‐lin.bin

Enter the Locale
1 (English)

Hit Enter 28 times to continue the agreement

Enter "Y" to accept the agreement

Enter the version to install
3 (Developer Edition)

Enter the next task to perform
1 (Server Configuration)

Enter whether you have an existing server configuration
2 (No existing server configuration)

Enter the next task to perform
5 (continue installation)

Enter the installation path
Hit enter to accept the default path

Hit Enter 30 times to continue the agreement

Enter "Y" to accept the agreement

Enter the serial number
Hit Enter for no serial number

Enter whether you h…