Ajax working only one account on wordpress - ajax

I have an issue when using Ajax in WordPress. Everything is fine when I'm using the main account (account with which I created database for my WordPress).
But when I am using another administrator account, Ajax does not work.

You may need to use the no priv option for the user who are not logged in.
wp_ajax_nopriv_<function_name>
Here function name is calling function name called from javascript/jquery.
$('.selector').on('click',function(){
var data={
action: "function_name",// function name used with no_priv
};
$.post(ajaxurl, data,function(res){
console.log(res); //print the output
});
});
Hope this will help you.

Related

Creating database entries in Exist-DB with Ajax calls

While developing a self-contained exist-db app I ran into some issues with passing on the user's identity during an ajax call.
I have created a standard page that allows the user to login in with his or her standard exist-db account. (It's more or less based on this https://github.com/eXist-db/existdb-login ).
At some point, the users need to create their own entries and upload files, and this is done with an ajax script (I'm using Kartik's bootstrap uploader, but it's basically a standard ajax upload function):
$('#input-24').fileinput({
uploadUrl: "../modules/uploader.xql",
uploadAsync: true,
fileActionSettings : {
uploadExtraData() {
return {
'FileName': $(this).data('title'),
}
}
},
});
Now, of course, this will fail, since it will appear like a GUEST user is trying to create entries, which is an action that is allowed only for registered users.
Does exist-db allow a way to send in my login credentials at this point without having to resort to the standard HTTP login (which is problematic, since it means creating a cookie to memorize the password, which more or less renders the whole login suing exist's mechanisms useless), or is there any way to use the controller instead of an external script?
Thanks in advance!
I think you can add the user and password into the URI to trigger the Ajax client to do basic with. e.g. http://username:password#your-server/some/uri/modules/uploader.xql
For simple, basic authentication we do the following since the page itself is being served from xQuery.
step 1: we create in the page xQuery a variable containing the session info. This should be fine for you ... the result of your login would be an HTML page:
let $sessinfo := util:base64-encode(concat(request:get-parameter('user', ()), ":", request:get-parameter('pass', ())))
step 2: during the build of the result page in xQuery, we turn that into a javascript variable through a simple script command placed in <head> tag:
<script>
var sessinfo = "{$sessinfo}";
</script>
step 3: in Javascript loaded with the page (we use jQuery), after the page is ready we setup authentication for all AJAX requests:
jQuery.ajaxSetup({
headers: {
"Authorization": "Basic " + sessinfo
}
});
After that, any AJAX request made would send basic authentication header with user and password to other xQueries executed with jQuery.ajax
To have an eXistdb session work with XHR (Ajax requests) you need make sure that
login:set-user('my.login.domain', (), false())
is called in the controller before you forward it to your request handler. Otherwise all request will seem to originate from the 'guest' user.
If you want to use vanilla/native JavaScript requests e.g. fetch you also need to tell it to add the credentials to the request:
fetch(url, { credentials: 'same-origin', method: 'GET' })
By the way, the persistent login used in exidtb-login likely uses a cookie value to pick up the session variables stored on the server (JSESSIONID), but I need to check that.

Passing session variable in ajax with coldfusion 9

I'm trying to create an online chat program for my web users to speak to a salesperson live. I use an ajax jquery command to refresh the chat, which is stored in a database. The problem I'm running into is it is losing the session variable that identifies the user on the ajax call, but it only seems to do this to some users. Is there some setting for coldfusion that I'm missing?
Should I have any specific settings set in my CF Administrator?
<cfapplication name="Chat Room"
clientmanagement="Yes"
sessionmanagement="Yes"
sessiontimeout="#CreateTimeSpan(0,1,0,0)#" >
<cfset session.UserID = #new_session.UserID# >
window.onload = function()
{
setInterval("ReloadChatWindow();", 2500);
};
function ReloadChatWindow()
{
$.ajax({url: "messages.cfm", success: function(result){
$("#ChatLog").html(result);
}});
$("#ChatLog").scrollTop($("#ChatLog")[0].scrollHeight);
}
new_session.UserID# is just from a database insert (the user provides their name and I assign them the userID.
Only one domain uses the session, there are no cross domain calls.
Session variables expire so you need to check if they are available before you can use them. In your messages.cfm file you have to check if the user still have a valid session and if it doesn't redirect them to the login page. If they do have a valid session you can update the messages.

Can't persist Auth::login with Laravel 5

I'm trying to manually log in a user within a Controller or a Route. This works during the scope of the page, but it doesn't persists.
Here is my code :
Route::get('check', function (){
if (Auth::loginUsingId(1)) { // User #1 is existing in the DB
return redirect()->intended('/check2');
}
});
Route::get('check2', function (){
dd(Auth::user());
});
And I got : null on my /check2 page
Can someone give me a quick hint?
PS: I tested the default Auth Laravel system and it works.
Thank you very much!
The issue was simple. On Laravel 5.2, you need to wrap all concerned Routes inside the middleware "web" in order to play with Authentification.
Thanks all.

Extend/Override jquery.ajax.done function to detect .net logon screen

I'm using forms authentication on an asp.net MVC application and I'm using jquery and ajax to create a rich user interface. Some users tend to leave the page open for hours and their login session is expiring as expected. If the user click a link or reloads the page the login screen is returned as expected. If they click a button or trigger an ajax call the ajax call is returning the login html instead of the expected html or JASON result. Resulting in a mangled page with half a login screen.
As I've already coded a lot of ajax calls I thought I could extend or override the default .done event to add a check for the login screen before continuing with whatever event I've programmed.
For example i have this function :
function CallAjax() {
$.ajax({type: "GET", url: "foo" })
.done(function (data) { $('#result').val(data); });
}
is it possible to override the default implementation of .done so do a check without rewriting this function? My check would probably to see if the result is a html response if so search for "login" to see if its the login page.
I would recommend you to extend the ASP.NET Forms Authentication module as Phil Haack explained in this blog post so that it no longer redirects to the login page when requested with AJAX but returns a 401 HTTP status code instead. Then you could simply use the .fail callback and intercept the 401 status code. The .done handler will not be invoked in this case.
I tried Darin's suggestion but after hours of trying I just couldn't get it to work in my set up. However I stumbled across this rather simpler solution which worked for me first time:
just add this to the global.asax
protected void Application_EndRequest()
{
var context = new HttpContextWrapper(this.Context);
if (FormsAuthentication.IsEnabled &&
context.Response.StatusCode == 302 &&
context.Request.IsAjaxRequest())
{
context.Response.Clear();
context.Response.StatusCode = 401;
}
}
}
Taken from http://jvance.com/blog/2012/09/21/FixFormsAuthentication302.xhtml

How to load the data using Ajax in Django template?

Using Ajax in Django is a open Issue. I have tried to understand it by reading blogs and forums, but it didn't work for me. I am posting a very simple question related to it.
Method defined in views.py: (just a sample)
def widget_data(request):
####
extra_context = {
'data': username
'part': company
}
return direct_to_template(request,'test/widgets.html',
extra_context)
I want to load extra_context to rendered template using Ajax.
Following things will happen in widget.html template i.e.
When a moderator will type a URL at the address bar to open a page it will load two widgets i.e. one for loading all the registered username and other one for their company name . user are continuously registering to the sites and adding company name to their profile. When a new user will registered to the site both widget should load automatically using Ajax.
I have no idea about the topic of Ajax.
How to do this?
How should i even start this?
I have read these following links :
Tutorial 1
Tutorial 2
I know that the answer will be too long and too messy but any help will be appreciative.
If you use jQuery you can do the following
$.get('/url/of/widget_data/view', success(data, textStatus, jqXHR) {
$('#idofdivtoupdate').html(data);
});
That would probably be the easiest way to do it.

Resources