Make ajax request to Control Method in Web Forms - javascript

I have control
LogInOut.ascx
which i use on different pages *.aspx
I need to add logic on button click.
I need to make ajax call to contol's method but i don't know how to get access to it.
What i try : LogInOut.ascx/validateUser, /validateUser, /Controls/LogInOut.ascx/validateUser, but always get error in ajax call
*( in debug mode i never was in this method, so problem in javascript).

You can't call a page method declared within an ASCX user control, use an ASMX web service.
http://forums.asp.net/t/1423555.aspx

Related

How to Run an XMLHttpRequest, Put Result in Session Before Page Loads

I want to run an XMLHttpRequest to grab information from an API and then use it to populate variables in PHP code that gets executed (without jQuery). While I would like to inject the information after the page loads, that's not possible in this case for various reasons. In essence, we're populating non-HTML PHP code with no CSS ID to select.
I tried creating a Promise and using then to inject the results of the API call into session variables, but I discovered that XMLHttpRequest deprecated synchronous requests on the main thread (e.g. you can no longer use false for the last parameter in the open function.
E.g.
xhr.open("GET", "https://www.myapi.com/api/v1/objects/1.json", false);
So, how would I go about setting these variables from XMLHttpRequest before the page is rendered? Is there a good way to do that or is it deprecated for a reason and we need to consider a deeper change?
Thanks in advance for any help!
Using synchronous calls was deprecated because is almost always possible to circumvent and it slows page loading and user's experience. Is it really necessary to do it before the page rendering? You can place a loader until the request is done and the callback has the data and then remove it.
Or even better if this happens only on page load PHP can just put the result in the output HTML, if you need to store it for JavaScript use you can echo something like:
<script type="text/javascript">
var globalVar = ['stuff'];
</script>
You can use it from other scripts, and also use window.globalVar to get the value because it is global.
If it is an api with a private key then it MUST be called by PHP rather than JavaScript, otherwise whoever analyzes the code could gather the API KEY, else if it is public and you don't want to overload the server with this requests the callback method is the way to go: place a loader with an overlay on the rest of the page, when the request is completed, do your calculations and delete or hide loader and overlay elements.

Pass PHP class instance through AJAX?

I recently wrote a PHP wrapper class for a new API we are using and have been asked to setup a demo which makes use of it. Certain features can be called directly from PHP, however things such as performing actions on button clicks requires I make use of JS/AJAX.
As I already have an instance of the object in my main PHP file, can I pass this as a parameter to JS and then pass it through Ajax to my handler or is it necessary to establish two separate instances?
The closest thing to doing what you want that I can think of would be copying the PHP object to a JavaScript variable via json_encode and then passing that variable back to your PHP code during the AJAX event via the data parameter. PHP code doesn't persist in the way that you seem to be describing - once a page has been requested by a browser, your PHP code for that page is done, there are no variables persisted by the server after that point.

Appending my jsession ID at every ajax call by Jquery

I am running my web application on Weblogic
application server.I am making several ajax calls in my application via Jquery UI $ajax and other Jquery Plugins (i.e. Jquery Datatables and JSTree)
Now i need to maintain same session in all these calls.Any user in the same session should only be allowed to do this.For this i am trying to append the JSESSIONID to every ajax call.
The problem with this approach is that i don't know how to get this value in my javascript file.If i write my code in JSP ,i can do that but in javascript i do not know of a way to do this.
Please help me with a way to set it into Jquery so that it gets passed with every request.
Thanks
The best solution is to put it in a session cookie, which will be passed with the AJAX requests and can be interpreted by the server. No changes needed to any JavaScript AJAX requests.
The next would be more complicated. You could write a jQuery.ajaxSend() function that checks the request url (to make sure it's one you need to add the session ID to) and add the sessionID to the data object before the AJAX request is made.

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.)

Set web part personalizable property from JavaScript

I have defined a personalizable property on a web part and I would like to modify its value from JavaScript. Is it possible?
You would have to use AJAX to send a message to the server. There's no way to set it from JavaScript without going to the server.
The AJAX could be as simple as a PageMethod call though.

Categories

Resources