Passing Local Storage Variable to AJAX URL? - ajax

How can I pass a local storage variable into the AJAX url:
My problem is the variable "url" won't pass into the url section where I put + url +
Any help would be greatly appreciated. Thanks John
<script id="source" language="javascript" type="text/javascript">
$(function ()
{
$.ajax({
var url = localStorage.getItem('iphoneusername');
url: 'http://' + url + '.com/iphone/adddisplayapi.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data[0];
var name = data[1];
var pic1 = data[2];
document.write(name);
document.write("<br/>");
document.write(id);
document.write("<br/>");
}
});
});
</script>

Try:
$(function () {
var urlParam = localStorage.getItem('iphoneusername');
$.ajax({
url: 'http://' + urlParam + '.com/iphone/adddisplayapi.php',
data: "",
dataType: 'json',
success: function(data) {
....rest of your code
Hope it helps

Related

laravel-excel: How to use progress bar when exporting

Now my download is ok. I think use a progress bar is better. I see this: https://docs.laravel-excel.com/3.1/imports/progress-bar.html
But it's for imports. And I want to use in blade, ajax. Can someone give me some suggestion?
<script type="text/javascript"><!--
$('#button-export-save').on('click', function () {
var dataString = $('#excel-form').serialize();
$.ajax
({
type: "POST",
url: "{{ route('lang.admin.member.members.export') }}",
data: dataString,
cache: false,
xhrFields:{
responseType: 'blob'
},
success: function(data)
{
var link = document.createElement('a');
link.href = window.URL.createObjectURL(data);
link.click();
},
fail: function(data) {
alert('Not downloaded');
}
});
});
//--></script>

How to Clear previous ajax response images before appending new images using ajax?

I want to clear my previous ajax response images before appending new images please help me how can i do that ? thank u.
Ajax
<script>
$(document).ready(function(){
var preloaded = [];
$(document).on('click','.editBtn',function(){
var marchandiseId = $(this).data("id");
var file ="{{Config('wfh.file')}}";
$(".input-images-edit").empty();
$("#editModel").modal('show');
var url = "{{URL('/marchandise/edit/')}}";
$.ajax({
type:"GET",
url: url + '/' + marchandiseId,
dataType:'JSON',
success:function (response){
var data = response.marchandise.marchandise_details;
$.each(data, function(i, item) {
preloaded.push({id: item.id, src: file + '' + item.images });
});
$('.input-images-edit').imageUploader({
preloaded: preloaded,
imagesInputName: 'photos',
preloadedInputName: 'old'
});
}
});
});

$.ajax() to $.getJSON()

I would like to ask if there is a method to parse data from $.ajax() to $.getJSON() please?
This is the $.ajax() example:
var apiUrl = 'https://api.com/url_here';
var apiKey = 1234567890;
var apiSecret = 0987654321;
var data = {};
$.ajax({
url: apiUrl,
headers: {
"Authorization": "Basic " + btoa(apiKey+ ":" + apiSecret)
},
data: data,
dataType: 'json',
type: 'GET',
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
});
And this is what I have in my $.getJSON(), where I have a struggle in calling the headers for authentication:
$.getJSON(apiUrl, function(json){
console.log(JSON.stringify(json));
});
Thanks a lot! xxx

AJAX Crashes Chrome

Umm.. my problem is that i want to use ajax for my chatbox but whenever i try to put ajax for no-reload refresh Chrome/Firefox Crashes down.. HERE's my code:
/chatlog.php/
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
//<script src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.0/mootools.js"></script>
<script>
var auto_refresh = setInterval(
function()
{
$.ajaxSetup({ datatype: "html" });
$('#loaddiv').load('chatlog.php');
}, 10000);
</script>
/submit.php/
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$(".submit").click(function(){
var message = $("#message").val();
var dataString = 'message'+message;
$.ajax({
type: "POST",
url: "submit.php",
data: dataString,
async: false,
});
/*$message=$_POST['message'];
$name = $_SESSION['username'];
$room = $_SESSION['room'];
$user = $_SESSION['user'];*/
});
});
</script>
$.ajax({
type: "POST",
url: "submit.php",
data: { message : message },
async: false
});
Also put your ajaxSetup outside of the setInterval function.
$.ajaxSetup({ datatype: "html" });
Pass your data properly, like:
var dataString = 'message'+message;
//Should Be
var dataString = 'message='+message;
//OR
var dataString = {'message' : message };

Facebook Subscribe Call Back

Facebook subscribe didn't call back. How callback successful?
<script> window.fbAsyncInit = function() {
FB.init({status: true, cookie: true, xfbml: true}); var user= "<?
echo $data->id;?>";
document.getElementById("Hint").style.display='block';
FB.Event.subscribe('edge.create', function(response) {
$.ajax({
type: "POST",
url: "fbareceive.php",
data: "data="+response + "---" + user,
cache: false
});
(function() { var e = document.createElement('script'); e.type =
'text/javascript'; e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js'; e.async = true;
document.getElementById('fb-root').appendChild(e); }());
</script>
$.ajax() is a jQuery function. Please read the relevant documentation: http://api.jquery.com/jQuery.ajax/
Briefly:
$.ajax({
type: "POST",
url: "fbareceive.php",
data: "data="+response + "---" + user,
cache: false,
success: function(){
alert('Success');
}
});

Resources