Am having a problem with my JavaScript log out code - javascript

I am trying to logout a user using a JavaScript code but it keeps loading without logging out.What have I done wrong?
I have tried changing the function itself but it still wouldn't log out.The loader just keeps on running.
this is the code
logout.html(loader);
$.get("modules/"+role+"/"+role+".php",{
},function(pagedata){
logout.show().html(pagedata);
});
I'm expecting the code to logout and take me back to the login page.

The
$.get()
method requests data from the server with an HTTP GET request.
The required URL parameter specifies the URL you wish to request.
The optional callback parameter is the name of a function to be executed if the request succeeds.
$.get(URL,callback);
so change your code :
logout.html(loader);
$.get("modules/"+role+"/"+role+".php",function(pagedata){
logout.show().html(pagedata);
});

Related

Oracle Apex get cookie from an ajax request

How can I get the value of a cookie in oracle from a request that was originated with ajax from a non-apex page (inside an apex server)?
I wanted to start by creating a function that returns the value of the login cookie and use that function to return the value to the browser to see that it can be done.
So I created a resource template and handler with the following source: select test() from dual.
The test function is declared as:
create or replace function test return varchar2
is
c owa_cookie.cookie;
begin
c := owa_cookie.get('LOGIN_USERNAME_COOKIE');
return c.vals(1);
end;
When I run select test() from dual from an sql commands window I get the value of the cookie, but when I send a request to the handler with ajax I get the following error:
An error occurred when evaluating the SQL statement associated with this resource. SQL Error Code 6502, Error Message: ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SYS.OWA_UTIL", line 354
ORA-06512: at "SYS.OWA_COOKIE", line 59
ORA-06512: at "SYS.OWA_COOKIE", line 179
ORA-06512: at "<no need to see this>.TEST", line 5
I've been using the following call (checking the result through the network dev tools)
$.ajax({url: "<handler url here>"})
Update:
The reason I'm using a rest service is because the page that originates the request is not an apex application but it is on the same domain (so it has the same cookies).
This is what I think is happening: The cookie is sent as part of the http request header of your apex application call. The rest call is another http request, unrelated to your apex http request and I don’t believe the header of that request includes the cookie. So in the database session that is created as part of your rest request the cookie will not exist. You could probably set the header of the rest request yourself to fix this.
--UPDATE--
To check which cookies can be read, create a rest handler of source type pl/sql with the following source:
DECLARE
l_names owa_cookie.vc_arr;
l_vals owa_cookie.vc_arr;
l_num_vals INTEGER;
BEGIN
owa_cookie.get_all(
names => l_names,
vals => l_vals,
num_vals => l_num_vals);
APEX_JSON.open_array('cookies');
FOR r IN 1 .. l_names.COUNT LOOP
APEX_JSON.open_object; -- {
APEX_JSON.write('name', l_names(r));
APEX_JSON.write('value', l_vals(r));
APEX_JSON.close_object; -- } cookie
END LOOP;
APEX_JSON.close_array; -- ] cookies
END;
The apex username cookie is not in that list of cookies. I suggest you create your own custom cookie in an after-login process in apex.
I verified that a cookie set with the following code in an apex process is picked up by the rest handler (based on blog https://www.apex-at-work.com/2012/05/apex-simple-cookie-example.html)
begin
owa_util.mime_header('text/html', FALSE);
owa_cookie.send(
name => 'CUSTOM_USER_COOKIE',
value => :APP_USER,
expires => sysdate + 30 );
apex_util.redirect_url (
p_url => 'f?p=&APP_ID.:1:' || :SESSION,
p_reset_htp_buffer => false );
end;
To get a cookie from an AJAX request within APEX, you can use the OWA_COOKIE package, but you do not need to define any templates or handlers. It can all be done from within the page (or calling an external procedure from within the page). Below are the steps I used to get the JSESSIONID cookie.
I have built an example application on apex.oracle.com that you can check out.
Page Setup
The page is very simple with just one text box and one button. The button's action is set to Redirect to URL and this is the target:
javascript: apex.server.process("AJAX_TEST", { }, { success: function (pData) { console.log(pData); apex.item("P10_JSESSIONID").setValue(pData); }, dataType: "text", error: function (jqXHR, textStatus, errorThrown) { } }).always(function () { });
The javascript is calling the AJAX Callback (which is shown below) and storing the output in the P10_JSESSIONID page item as well as logging it to the browser console.
AJAX Callback Setup
The code in the AJAX Callback is very similar to the code you shared. I am getting the cookie then printing it out with HTP.P. This is all that is needed to return the value of the cookie to the page to be used for whatever you'd like. You can also make the code in the AJAX callback call a standalone procedure compiled into the database if you so choose.
Update
I updated my example application so that there will be a dropdown showing all of the cookies, then you can select from the list and click the button to send an AJAX request to get the value of that cookie.

How to abort called ajax method c# .net

I have a ajax.cs class and I am calling methods from javascript side. So for this I registered it on default.aspx page load like this.
Ajax.Utility.RegisterTypeForAjax(typeof(ajax));
Sometimes some methods taking a long time, At that time I want to abort this call. How can I abort this request?
I call method like this from js;
ajax.testMethod()
If you make your controller action return a Task, then you can have cancellation token as a parameter to the action. And this cancellation token is automatically triggered when the HTTP request is aborted. And in the action, you can handle the token however you like to cancel the long-running operation.
Not sure if it works, but instead of aborting the ajax from the C# side, why don't you cancel it from the js side? in your ajax configuration, you can add a timeout parameter which, when happens, triggers the .error() function you can define, or leave it blank if you just want it to stop executing.
For js:
var ajax = new XMLHttpRequest();
ajax.post('your url');
ajax.timeout(10000); //or any time in milliseconds to set a timeout.
ajax.ontimeout= function(){ }//optional, just if you want a callback when ajax times out
For jQuery:
$.ajax({
url:'yoururl',
timeout: 10000,
error: function(){
}
});

Ajax sending response back to php after ajax call

Suppose I have a page called form.php. I then clicked a button called "add button". This button triggers an event that got detected by a jquery function. The jquery function makes an ajax call to add.php.
Inside add.php, there is code that checks if a particular record exist in the database. If it does find that the record exists, I want to do the following.
Send a response string "exist" to ajax.
The ajax, inside the .done() function, will execute a prompt that says "This record already exist, do you wish to overright"?
If the user canceled the prompt, nothing more should happened and the ajax call should be done.
If the user clicks "ok", I would like the php script to be notified of this and execute an update statement using the data from form.php.
I suspect this is impossible because after receiving a response from php, AFAIK there is no way for ajax to respond back to the php script that is currently executing.
Am I correct or there is a way to do this?
You have to add a parameter to your ajax request, like override with true and false. By default/first request you set it to false. Then the add.php does it's default and returns exists.
The the user makes his decision. If he want to override, you send the ajax request again with the override parameter to true. Your add.php will notice the parameter and does whatever it has to do.
Wrap your ajax handler in an own function with a done callback. So you can reuse the request as often as you want. Pretty easy, no double code needed as well ...
The .done() function of your first ajax call executes when the ajax call has finished successfully, so when your php script has finished completely.
If you want to do something else, you would need to make a new ajax request. That could be to the same or another script, sending in different / new / additional parameters.
Note that you have to make sure that the second script cannot be called without the first one finishing, for example by setting and checking an expiring session variable.
you can do something like this.
$.post('add.php',$(this).serialize())
.done(function(result){
var r = confirm("This record already exist, do you wish to overright");
if(result == 'exist'){
if (r == true) {
$.post('update.php',$(this).serialize()).done(function(r){
console.log(r);
});
} else {
return false;
}
}else{
console.log(result)
}
});

Results dissapearing on refresh

After the user submits a form a new view for the results will be displayed. The results view will use the form fields to create a JSON object, send the JSON with an ajax request to the server and receive a JSON response that has all of the results. The results are then rendered with the view. This all works fine but when the results page is refreshed all of the results are gone. How would I make it so that my results will continue to show up after I refresh the page? What I'm trying to do now is alter the URL so that it will contain the query and then the results view will use the URL to form a request and send it to the server. Is this the recommended course of action for what I'm trying to achieve here? Thanks.
You will need to call the function which calls the ajax request on page load.. your values are getting lost as it will reset to default undefined/null etc when the user loads the page again.
If you want the onload method to take parameters, you can do something similar to this:
window.onload = function() {
// put something here so the function will not call if the ajax call has not been called beforee
yourFunction(param1, param2);
};
This onload will call the function for you therefore no matter what running the ajax call each time.
of course you will need validation so it does not call that function if the ajax request has not been used before.i.e. something in the query string when its successful etc.

Requesting something via ajax - how to stop other requests until this one is done

I have a php script that outputs json data. For the purposes of testing, i've put sleep(2) at the start.
I have a html page that requests that data when you click a button, and does $('.dataarea').append(data.html)
(php script returns a json encoded array. data.html has the html that i want to put at the end of <div class="dataarea">...HERE</div>.
The trouble is, if i click the button too fast (ie. more than once within two seconds (due to the sleep(2) in the php script)), it requests the php file again.
how can i make it only do one request at a time?
i've tried this (edited down to show the important parts):
amibusy=false;
$('#next').click('get_next');
function get_next() {
if (amibusy) {
alert('requesting already');
}
else {
amibusy=true;
// do the request, then do the append()
amibusy=false;
}
}
but this doesn't seem to work. i've even tried replacing the amibusy=true|false, with set_busy(), and set_not_busy(). (and made a function am_i_busy() { return amibusy; })
but none of this seems to work. what am i missing?
If you're in jQuery the amibusy would be jQuery.active which contains a count of currently active AJAX requests, like this:
if(jQuery.active > 0) { //or $.active
alert('Request in Progress');
}
Keep in mind that in jQuery 1.4.3 this becomes jQuery.ajax.active.
Disable the button in the click event and enable it again when the request is finished. Note that the request is asynchronous (i.e. "send request" returns immediately), so you must register a function that is called when the answer comes in.
In jQuery, see the load() function and the success method plus the various AJAX events which you can tap into with ajax().
I'm wondering about your "do request" logic. Whenever I've done calls like this they've always been asynchronous meaning I fire the request off and then when the response comes another function handles that. In this case it would finish going through that function after setting the callback handler and set your value of amibusy back to false again before the request actually comes back. You'd need to set that variable in the handler for your post callback.
Could you use the async variable?
http://api.jquery.com/jQuery.ajax/
asyncBoolean Default: true
By default, all requests are sent
asynchronous (i.e. this is set to true
by default). If you need synchronous
requests, set this option to false.
Cross-domain requests and dataType:
"jsonp" requests do not support
synchronous operation. Note that
synchronous requests may temporarily
lock the browser, disabling any
actions while the request is active.

Categories

Resources