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 VB.net.
 _
 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 … 
        AddError(error.get_message());
    }

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

Comments


  1. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    Selenium training in Chennai

    ReplyDelete
  2. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.thanks for your information really good and very nice web design company in velachery web design company in chennai

    ReplyDelete
  3. Your topic is very nice and helpful to us … Thank you for the information you wrote.

    Learn Hadoop Training from the Industry Experts we bridge the gap between the need of the industry. Softgen Infotech provide the Best Hadoop Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.
    Big Data Analytics Training in Bangalore
    Tableau Training in Bangalore
    Data Science Training in Bangalore
    Workday Training in Bangalore


    Learn Hadoop Training from the Industry Experts we bridge the gap between the need of the industry. Softgen Infotech provide the Best Hadoop Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.
    Big Data Analytics Training in Bangalore
    Tableau Training in Bangalore
    Data Science Training in Bangalore
    Workday Training in Bangalore

    ReplyDelete

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 app location -  https://code.google.com/apis/console Switch on the "Blogger API v3" Get the latest APIs client library for PHP location -  https://code.google.com/p/google-api-php-client/downloads/list Upload the files to your host location on on localhost Extract 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 se

My two cents on new year resolution

What is the plan for the new year ? - need to think on what are we trying achieve during next year 2018 - basically the life goals - may be personal or professional - and also it should be realistic (not something like going to Mars ;)) Why we need a plan for the new year ? - basically a goal without a plan is a DREAM - And also should be able to measure (what you cannot measure, you cannot manage) How to prepare a new Year resolution/plan ? - Leave some buffer time - Make changes during the year (life is changing/evolving) - Plan is only for you (do not share it) - When a milestone is achieved, celebrate - Try to stick to the plan - otherwise no point of planing

Assets and Liabilities as Rich Dad, Poor Dad explains

I was reading "The rich dad poor dad by Robert Kiyosaki" here is a one point that he mentions on that. Basically Asset as he says is little bit different than on books. If something puts money in your pocket it is a asset. And Liabilities are the ones that takes money out of your pocket. OK for example a house or a car may seems like an Asset but it takes money out of you pocket to maintain them. But if you rent them or make them to make money at the end of the day you can convert it to a asset. Basically that what rich people do. They buy assets. Middle class buy liabilities (thinking those are assets) and stuff (a lot of them that not used or that not needed). Lower class buy to consume (basic needs like foods).