I create simple safari extension which used simple js file where i send request on my own server and check user session is set or not. In my extension code when i send Ajax call to my own server it get other information but can't get user session while user is logged in.Here is my code:
var storeUrl = window.location.href;
$(document).ready(function()
{
var store = storeUrl.replace("http://",'');
store = store.replace("/",'');
jQuery.ajax({
url: 'https://www.karmora.com/checkstore/' + store,
context: document.body,
error: function(data, transport) {
},
success: function(data) {
var storeData = jQuery.parseJSON(data);
console.info(storeData);
}
});
});
Related
I am very new in laravel. I have used javascript for auto logout functionality. The problem is when I am switching account that creates a problem. The scenario is as follows:
Logged in as a Superadmin
After login, there is a list of ServiceProvider. Clicking on it will impersonate them and the session data will be replaced to that service provider.
So now you are logged in as a service provider without logging out from the superadmin account. In this case, the ajax call showing 419 unknown status and reloading the page is logging out from that account.
I did search for that 419 unknown status but all of them are showing the same solution of passing the CSRF token. In my case, I am already passing it but still, it is showing the error only when I am switching the account.
Really need some solution on this. Thanks in advance.
Following is my auto logout code:
layout/app-inside.blade.php
<script>
var sesnLifeTime = 10 * 60;
var logtime = "{{strtotime(Session::get('logtime'))}}";
var lastActvTime = logtime;
$( "body" ).bind( "mousemove", function() {
var date = moment.utc().format('YYYY-MM-DD HH:mm:ss');
var stillUtc = moment.utc(date).toDate();
var nowDateTime = moment(stillUtc).local().format('YYYY-MM-DD HH:mm:ss');
var nowDateTimestamp = moment(nowDateTime).format("X");
lastActvTime = nowDateTimestamp;
});
document.addEventListener("DOMContentLoaded", function (event) {
setInterval(function(){
var date = moment.utc().format('YYYY-MM-DD HH:mm:ss');
var stillUtc = moment.utc(date).toDate();
var nowDateTime = moment(stillUtc).local().format('YYYY-MM-DD HH:mm:ss');
var nowDateTimestamp = moment(nowDateTime).format("X");
var idleTime = parseInt(nowDateTimestamp) - parseInt(lastActvTime);
if(idleTime >= sesnLifeTime)
{
$('#myModal').modal('show');
console.clear();
console.log("idleTime ==== "+idleTime);
}
else
{
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: '/dashboard.setTime',
type: 'POST',
async: false,
data:{'lastActvTime': nowDateTime},
success: function(response) {
console.log(response+"===="+idleTime);
}
});
}
}, 5000);
});
</script>
The above ajax is calling from the follwong controller.
DashboardController
function setTimeAjax(Request $request) {
$lastActvTime = $request->lastActvTime;
//== storing and fetching the updated time into session ==//
$request->session()->put('logtime', $lastActvTime);
$data = $request->session()->all();
//== storing and fetching the updated time into session ==//
$logtime = $data['logtime'];
echo $logtime.'===='.$lastActvTime;
}
I am checking user activity every 5 seconds. So when I am switching account, instead of logging me out, it should be remain logged in as I am the active user.
I'm using a Web API to fetch my document using the following:
[Route("Api/DocumentApi/DownloadDocument")]
[HttpGet]
public IHttpActionResult DownloadDocument(int documentID)
{
Document documentToDownload = new Document();
using (TrustInvestmentSwitchEntities db = new TrustInvestmentSwitchEntities())
{
DocumentRepository repo = new DocumentRepository();
documentToDownload = repo.GetSingle(db, x => x.ID == documentID);
}
var stream = new MemoryStream();
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(stream.GetBuffer())
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = documentToDownload.FileName
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = ResponseMessage(result);
return response;
}
This looks like its working at retrieves the document. However, I want the document to either download immediately or show a popup for the user to select a location to save the file and this is not happening. Download immediately is preferred.
Here is my Javascript GET which I think is the problem:
DocumentToDownload = $(that).closest('.document-item').data('documentid');
var url = '/Api/DocumentApi/DownloadDocument';
var data = {
DocumentID: DocumentToDownload
};
$.ajax({
type: "GET",
url: url,
contentType: "application/json",
data: data,
dataType: "json",
success: function (json, status) {
if (status != "success") {
log("Error loading data");
return;
}
log("Data loaded!");
},
error: function (result, status, err) {
log("Error loading data");
return;
}
});
Im unsure what to put after:
success: function (json, status) {
Ajax file downloads are not allowed for security reasons (otherwise any site could download any file to the users machine in the background)
No need to use an ajax call, you can trigger the download without reloading the page using a normal link if the href is pointing to a URL that returns a document (the header is a document) which it looks like your API is doing. So you could simply do:
Download
Where the DocumentID is set to the ID of the document you want to download. When the user clicks the link the page won't change/refresh
i am trying to add member on myewellness.com
i am using jquery and php on a wordpress website, i have to collect the user's details and add him to myewellness.com
this is the link to their api documentation:
http://www.mediafire.com/view/pr6s7isht9ihdao/API-Services-Documentation-v2.0-1.pdf
but i am getting this on chrome:
OPTIONS https://api.myewellness.com/api/v1/services/membership/create send # jquery.js?ver=1.11.2
(index):1 XMLHttpRequest cannot load https://api.myewellness.com/api/v1/services/membership/create. Response for preflight has invalid HTTP status code 405
(index):1509 Failed: [object Object]
and CORS not enabled error on fire fox,
here is my js:
jQuery.noConflict();
(function( $ ) {
$(function() {
$("#register_form").submit(function(){
console.log("Wellness API Initiated!");
var api_user_name = $("#user_name").val();
var api_user_email = $("#user_email").val();
var api_payment_first_name = $("#payment_first_name");
var api_payment_last_name = $("#payment_last_name");
var api_user_password = $("#user_password").val();
var api_client_address_one = $("#client_address_one").val();
var api_client_address_two = $("#client_address_two").val();
var api_client_city = $("#client_city").val();
var api_client_state = $("#client_state").val();
var api_client_zip = $("#client_zip").val();
var api_client_gender = $("#client_gender").val();
var api_client_home_phone = $("#client_home_phone").val();
var api_client_cell_phone = $("#client_cell_phone").val();
var api_client_work_phone = $("#client_work_phone").val();
var api_client_birth_date = $("#client_birth_date").val();
$.ajax({
type: "POST",
url: "https://api.MYEWELLNESS.com/api/v1/services/membership/create",
/*headers: {
'Authorization':'Basic Vml0YWxhbGVydA==:MTI5OTky',
'Content-Type':'application/json'
},*/
data: "user_name="+api_user_name+"&password="+api_user_password+"&first_name="+api_payment_first_name+"&last_name="+api_payment_last_name+"&address1="+api_client_address_one+"&address2="+api_client_address_two+"&city="+api_client_city+"&state="+api_client_state+"&country=US&zip="+api_client_zip+"&email="+api_user_email+"&phone="+api_client_home_phone+"&work_phone="+api_client_work_phone+"&cell_phone="+api_client_cell_phone+"&gender="+api_client_gender+"&birthdate="+api_client_birth_date,
datatype: "json",
beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " +"Vml0YWxhbGVydA==:MTI5OTky"); },
success: function(msg){
console.log("Success: "+msg);
},
error: function(error){
console.log("Failed: "+error);
}
});
console.log("API Fire Complete!");
return false;
});
});
})(jQuery);
Their server isn't giving you permission to instruct other people's browsers to access their API.
You shouldn't want to do that anyway - otherwise you will be handing over your API key to all your visitors!
Make the API requests from your server, using PHP, instead. You'll probably want to look at the cURL library to do that.
I am using Facebook SDK 4.0 and I have a login page in codeigniter using AJAX to send and receive requests and login users.
I would like to allow users to login using Facebook, but I'm stuck. I have a form with action to controller function named facebook_login. I have a javascript on submit of this form:
$('.facebook_login').submit(function() {
var errors = $(this).find('.errors');
var success = $(this).find('.success');
var overlay = $(this).find('.overlay');
overlay.fadeIn(300);
errors.fadeOut('fast');
success.fadeOut('fast');
var url = $(this).attr('action');
var data = $(this).serialize();
$.ajax({
url: url, //url = path to which form should be submitted
type: 'POST', //POST = form method
dataType: 'json', //json - data type in which server is supposed to send response
data: data,
success: function(response) { //response = reply sent by server after processing our request
if (response.status == true)
{
errors.fadeOut('fast');
overlay.fadeOut('fast');
success.html(response.message).fadeIn('fast').delay(300).fadeOut(function(){
window.location =response.redirect;
});
}
else
{
success.fadeOut('fast');
overlay.fadeOut();
errors.html(response.message).fadeIn('fast');
}
},
error: function() {
overlay.fadeOut();
}
});
return false;
});
The function facebook_login does this:
session_start();
$this->load->library('facebook');
$user_logged_in = $this->facebook->logged_in();
//check if the user is already logged in with facebook
if(!$user_logged_in){
//if the user is not logged in
$loginUrl = $this->facebook->login_url();
}
I want to redirect the user to login to facebook and then gets redirected back to the website, save or session the user and display an error/successful alert using:
header('Content-Type: application/json');
echo json_encode($response);
Simply use javascript redirect.
location.href = "http://some.com/your path";
Managed by using the JS SDK with the PHP SDK thank you #luschn
I am developing a website in which only 1 html page is there in which I first fethch the url & gets the id from url and send it to api using ajax call. On success, I displays data of the given id from url.My code is as-
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#main').hide();
var url = window.location.href;
var formno = url.substr(url.lastIndexOf('/') + 1);
if (formno != 'Login.html') {
var apiUrl = 'http://localhost:801/api/api/Patient/Get';
$.ajax({
url: apiUrl,
crossDomain: true,
contentType: "application/json",
type: 'GET',
data: { formNo: formno },
success: function (result) {
$('#main').show();
alert("Success" + result)
},
error: function (result) {
alert("error :: " + JSON.stringify(result))
}
});
}
});
</script>
when I use the url as abc.in#1 it displays the success alert but I want to give the url in format abc.in/1 at that time it gives
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Why it can not find the page? Is there any solution for this?
I want to give plain url as abc.in/1 where 1 is id and which is dynamic.
Is there any solution?
Your browser is probably trying to access document on location abc.in/1, which doesn't exist. You will need some server side logic for this, e.g. php router which will always serve your document, and additonal parameters will be processed by it. abc.in#1 anchor is different type of url parameter, which purpose is to be processed by document or javascript on client side.