PageMethods with ASP.Net MVC - javascript

I have found ASP.Net PageMethods very handy and easy to use, but I have just started developing using MVC and am not sure how to use them?
What is the equivalent of PageMethods.MyFunction() in MVC where MyFunction is a Controller action?
I know I can use the Json function to return a value, but how do I call the action from the client?

I know I can use the Json function to
return a value, but how do I call the
action from the client?
I think you're looking for either getJSON
$.getJSON("/controller/action", function(json)
{
alert("JSON Data: " + json.users[3].name);
});
or the ajax jQuery method.
Either can call an action and get JSON data back from ASP.NET MVC very easily.

I don't think you need page methods. Page methods in asp.net are a way to expose methods in the page class to your client-side code.
In MVC, you don't have a page class, so you can just issue an XHR to a url (../controller/action/params, or whatever), and return JSON from the action.
Update:
After re-reading your question, it sounds like you want to know how to issue an XHR from the client. In raw javascript, you can just use the XMLHttpRequest object, but whatever JS library you are using probably has a nicer wrapper. jQuery's, for instance, is here

Related

Converting a table from the JSP to Jquery data table

I am returning a JSP, which will have a table generated depending on the data attached with the model. I want to convert the table to a JQuery data table after the JSP is loaded. How can I achieve this?
In fact I think it may be more adequate do already use a JQuery Datatable in your JSP from the start and not doing any further conversions when JSP is loaded.
If you are to consider that approach, your work would be to create an integration model between your Datatables component and your controller, which I am assuming is Spring MVC since Spring is tagged in your question.
That being said, your roadmap would be something like:
Create value objects to move datatables parameters back and forth between your JSP view and your Spring Controller;
Create your controller, that are going to handle data requests. This controller must be able to convert your incoming HttpRequest parameters to your value object. If you are indeed using Spring, your Spring Controller would use a customized WebArgumentResolver which are going to read the request and return your VO. This controller must handle your request and retrieve whatever response you might have to provide. Oh, also don't forget to resolve that Resolver in your MVC configuration.
Finally, your response would be a JSON, understandable by Datatables.
This is a great tutorial, which I used the first time I needed to implement such integration. Might fill some details for you.
Best regards.

Calling bean method using javascript

I want to show a list of options to the user when he/she clicks on an inputText component. I need to call a bean method by JavaScript using onclick attribute in IceFaces.
<ice:inputText id="inputText1" partialSubmit="true" value="" onclick="" />
How can I achieve this?
As #Neall said, you need to initiate an XMLHttpRequest and return the data to the client. There are many ways to do this and I don't know the framework you are referring to, but in general, you initiate the XMLHttpRequest passing some parameters -if needed- to a web method, for example, and then return the data in JSON format. When you issue the request it usually has a callback function for success and one for error. On the sucess event, you parse the JSON response and do whatever you need to do with it.
Look at here, for example:
http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/
It looks like you are trying to run server-side code when the user takes some action on the client side. You probably want to initiate an XMLHttpRequest.
The XMLHttpRequest basically just hits a URL, optionally returning some data to the browser. This is what people usually call AJAX. (For Asynchronous Javascript And XML - although people usually use JSON instead of XML.)

ASP.NET MVC + Javascript (calling c# code with javascript variables)

is it possible to call c# functions with javascript parameters (from javascript)?
i.e.
<script>
function someFunction()
{
var data = "123";
var test ="<% AppHelper.GetSomething("+data+"); %>";
}
</script>
Thank You !
No, because ASP.NET code is run and compiled before you get to the javascript. If you want to communicate with the server using javascript, you need to use something like AJAX.
Or you might have a look at server side javascript, to see if that's something you can use.
You could do it but you would have to use an ajax call to hit a URL endpoint which will execute an action. Once the action has been triggered you can do what ever you want in c#.
What you want to do exactly because with jQuery it's very easy to do a call to a controller that return some HTML or data that you can use in javascript. I could give you an exemple if you give more details of what you are trying to do.

How can I send Javascript events back to Rails to process?

If I have user interactions in my Javascript layer, can I have Javascript actions trigger Rails controller actions and pass data from Javascript to those Rails methods?
Yes. You make asynchronous calls back to your Rails application using XMLHttpRequest, typically through Prototype or some other Javascript library. You pass data back to the server using query parameters, much like any other request, and your application returns HTML fragments or Javascript code that is used by the browser to update the relevant parts of the page.
The PrototypeHelper class is useful for generating the right stuff in the server. Ajax.Request is what you'll use on the client.
I believe you can make AJAX requests back to your controllers. Rails by default includes prototype so you can use its AJAX functionality ( http://www.prototypejs.org/api/ajax/request )

Javascript jquery to contact an API

I'm trying to connect to a webapi at a location that looks like this from inside my js jquery file.
example.com/?var=input
Is there a simpler way to do it than an ajax call?
I would use AJAX for this, but I guess you could open a hidden IFRAME with the URL set to the page you want to connect to (not sure why you would do this though!).
Maybe use a JavaScript library like JQuery to make life easier?
If the data you're trying to access is returned as JSON then you can get around the browser security problems.
Here is a JQuery example where a request is made to Flickr.com from JQuery.com:
docs.jquery.com/Ajax/jQuery.getJSON
You may be run into cross domain issues if you do it with an ajax call.
Call the web-api from serverside, it would be the most appropriate way.

Categories

Resources