How can I get imgur.com album images using ajax request - ajax

I used this code :
(function($){
var albumID = 'NNbeO';
var albumAPI = "https://api.imgur.com/3/album/" + albumID + "/images";
$.ajax({
url: albumAPI,
headers:{
'Authorization':'xxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
type: 'GET',
dataType: 'json',
success: function(data) {
alert(data.data[0].link);
},
error: function() { console.log("ERRORZ"); }
});
})(jQuery);
But I got this error :
{
"data": {
"error": "Malformed auth header",
"request": "\/3\/album\/NNbeO\/images",
"method": "GET"
},
"success": false,
"status": 403
}

I got my solution. It works fine now. Below is my working code. It's problem was, I've not added Client-ID text with Authorization headers.:
(function($){
var albumID = 'NNbeO';
var albumAPI = "https://api.imgur.com/3/album/" + albumID + "/images";
$.ajax({
url: albumAPI,
headers:{
'Authorization':'Client-ID xxxxxxxxxxxxx'
},
type: 'GET',
dataType: 'json',
success: function(data) {
alert(data.data[0].link);
},
error: function() { console.log("ERRORZ"); }
});
})(jQuery);

Related

Problem with AJAX Post request in laravel

I have a select box and when each item is selected, a ajax request is sent.
but my code does not work. i'm get status 404 and this error is displayed in the console
exception: "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", file: "F:\\source\\boiler\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\RouteCollection.php", line: 179, …
my route :
Route::group(['namespace' => 'BuyCrypto' , 'prefix' => 'crypto'], function() {
Route::post('/calculateBuyAmount' , [BuyCryptoController::class , 'calculateAmount'])->name('calculate.amount');
});
ajax code :
$("select#user_select_crypto").change(function(e) {
$('#calculat_user_buy').block({
message: '<i class="icon-spinner4 spinner"></i>',
overlayCSS: {
backgroundColor: '#fff',
opacity: 0.8,
cursor: 'wait'
},
css: {
border: 0,
padding: 0,
backgroundColor: 'transparent'
}
});
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
e.preventDefault();
$.ajax({
url: "panel/crypto/calculateBuyAmount",
dataType: 'json',
data: {
user_select_crypto: $("input[name=user_select_crypto]").val(),
user_value_request: $("input[name=user_value_request]").val(),
},
success: function(result) {
console.log(result)
},
error: function(result) {
console.log(result)
},
});
});
what is problem?
this is because laravel not getting the full URL of your giving route. you have to give the correct route. for this, you can use your named route like below.
url: "{{ route('calculate.amount') }}",
or in case you are in a javascript file and can't use blade than just give URL with / slash. all is going to work. Its happen with a post request.
you can give URL like below
url: "/panel/crypto/calculateBuyAmount",
Updated
Now you facing methodNotAllowedException because now you sending a get request because you are not mentioning in ajax side you are doing a post request. by default ajax will send a get request. so you need to tell ajax use post method. like that type: "POST" like below
$.ajax({
url: "/panel/crypto/calculateBuyAmount",
dataType: 'json',
type : 'POST'
data: {
user_select_crypto: $("input[name=user_select_crypto]").val(),
user_value_request: $("input[name=user_value_request]").val(),
},
success: function(result) {
console.log(result)
},
error: function(result) {
console.log(result)
},
});
In this kind of situation, it is good to use named route as you give your route a name.
$.ajax({
url: "{{ route('calculate.amount') }}", // use your name route here
type : 'POST', // need to add your request type post
dataType: 'json',
data: {
user_select_crypto: $("input[name=user_select_crypto]").val(),
user_value_request: $("input[name=user_value_request]").val(),
},
success: function(result) {
console.log(result)
},
error: function(result) {
console.log(result)
},
});

Ajax form data passing to API

Below code not going to succeed, it is showing failure message.. anybody can help
$.ajax({
type: 'GET',
url: 'xyz.com/form_api.php',
data: {
name: 'John',
email: '123333',
mobile: 'deep#gmail.com',
message: 'Test'
},
success: function(response) {
console.log(response);
$('#contact_form #contact_results2').html('Success');
},
error: function(errorThrown) {
console.log(errorThrown);
$('#contact_form #contact_results2').html('failed');
}
});
Try this code:
$.ajax({
type: 'GET',
url: 'xyz.com/form_api.php',
data: { 'name': 'John', 'email': 'deep#gmail.com', 'mobile': '123333', 'message': 'Test'
},
success: function(response)
{
console.log( response );
$("#contact_form #contact_results2").html("Success");
},
error: function(errorThrown)
{
console.log( errorThrown );
$("#contact_form #contact_results2").html("failed");
},
});

CSRF token error? on laravel Symfony\\Component\\HttpKernel\\Exception\\HttpException

first was ok. second got error
I use the ajax function on javascript page in laravel
If I initiate the function once it work well
But when I start the function 2 or 3 times in short time I got the error
"exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException",
"file": "D:\\AppServ\\www\\come\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php",
I search the error message . The result is the csfr issue.
But how can I fix the error?
I have already have the
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
The question is not on the first time . It's on the second or third times.
Code
$('.findNews_geography').autocomplete({
source: function(request, response) {
var findtable=$('.findtable_num').val();
var terms=request.term;
console.log("findtable="+findtable+";term="+terms);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "findNews_geography",
dataType: "json",
type: "post",
data: {
findtable : findtable,
term : terms,
},
error: function(xhr, ajaxOptions, thrownError) {
console.log("findNews_geography ajax error="+xhr.responseText);
console.log("findNews_geography xhr.status="+xhr.status+";thrownError="+thrownError);
},
success: function(data) {
console.log("see request="+data);
response( $.map( data, function( item ) {
return {
label: item.place,
}
}));
} //success end
}); //ajax end
}, //source end
minLength: 0,
}); //autocomplete end
$(".findNews_geography").focus(function () {
//if (this.value == "") {
console.log("findNews_geography get focus");
if($('.findtable_num').val()){
$(this).autocomplete("search");
}// };
});
$('.findNews_geography').autocomplete({
source: function(request, response) {
var findtable=$('.findtable_num').val();
var terms=request.term;
console.log("findtable="+findtable+";term="+terms);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "findNews_geography",
dataType: "json",
type: "post",
data: {
findtable : findtable,
term : terms,
_token: $('meta[name="csrf-token"]').attr('content')
},
error: function(xhr, ajaxOptions, thrownError) {
console.log("findNews_geography ajax error="+xhr.responseText);
console.log("findNews_geography xhr.status="+xhr.status+";thrownError="+thrownError);
},
success: function(data) {
console.log("see request="+data);
response( $.map( data, function( item ) {
return {
label: item.place,
}
}));
} //success end
}); //ajax end
}, //source end
minLength: 0,
}); //autocomplete end
Try to send the csrf token in your ajax request as data
data: {
findtable : findtable,
term : terms,
_token: $('meta[name="csrf-token"]').attr('content')
},
Hope this helps

Typeahead.js Get ID

sorry but i don't find reply..
My problem is i can't retrieve the id of the selected product. The value = 0
My code :
$("#recherche").typeahead({
onSelect: function(item) {
alert(item.value); // = 0
},
ajax: {
url: "/personne/autocompletation",
displayField: "nomComplet",
triggerLength: 1,
method: "POST",
dataType: "JSON",
preDispatch: function (query) {
return {
query: query
}
}
},
});
My code HTML
<li class="active" data-value="0"><strong>C</strong>alloway</li>
Sorry for my english...
Try this
$("#recherche").typeahead({
source: function(query, process){
$.ajax({
url: "/personne/autocompletation",
displayField: "nomComplet",
triggerLength: 1,
method: "POST",
dataType: "JSON",
success: function (data) {
console.log(data);
}
});
}
});

Can not pass value from Ajax to controller

My AJAX:
$.ajax({
url: '{{ URL::to('dashboard') }}',
type: 'GET',
data: { cid: val },
dataType:'JSON',
success: function(result) {
},
error: function(){
$('#status-msg').addClass('alert alert-danger');
$('#status-msg').text('Fejl!!');
}
});
My Route:
My Controller:
public function dashboard(){
if (Request::ajax()){
$cid = Input::get('cid');
var_dump(json_encode($cid));
} else {
echo "XX";
}
}
The cid is not passing to controller, the request AJAX is not working.
have you try change your ajax type to POST ?
$.ajax({
url: '{{ URL::to("yourURL") }}',
type:'POST',
data:"cid="+$(this).val()+"&_token=" + $("input[name=_token]").val(),
dataType:'JSON',
success:function(result){
},
error: function() {
}
});
Edit:
try to pass _token if you use {{Form::open}}.
hope this will help.
Try this:
$.ajax({
url: '<?php echo base_url() ?>index.php/contorller_name/method_name',
type: 'POST',
data: { cid: val },
dataType:'JSON',
success: function(result) {},
error: function() {
$('#status-msg').addClass('alert alert-danger');
$('#status-msg').text('Fejl!!');
}
});

Resources