pass function parameter into ajax url doesnt' work? - javascript

$scope.getPostItem = function(itemLength){
// load post items ajax
$http({
method: 'GET',
url: '../Rresume/loadPostItem.php?itemLength=itemLength'}).
success(function(data, status, headers, config) {
});
trying to pass itemLength into the url, possible? I got a null result. But when I put itemLength=4 it work.

scope.getPostItem = function(itemLength){
// load post items ajax
$http({
method: 'GET',
url: '../Rresume/loadPostItem.php?itemLength=' + itemLength}).
success(function(data, status, headers, config) {
});
Try this

Related

Adding $http request bricks Ionic app

I'm trying to add a post request to a button in my ionic app but I've found that adding the code makes the app useless. Not a single thing responds to any of my taps. Removing the code makes things normal again. This is all I have added:
$scope.powerPrompt = function() {
var pwr = alert("Power On");
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$http({
method: 'POST',
url: 'url',
data: "string"
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(response) {
// handle success things
})
.error(function(data, status, headers, config) {
// handle error things
})
};
If I delete everything inside the powerPrompt function except the var pwr line, the rest of my app works. What's causing this? Do I have a syntax issue?
You are missing a , after the data property
$scope.powerPrompt = function() {
var pwr = alert("Power On");
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$http({
method: 'POST',
url: 'url',
data: "string",
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(response) {
// handle success things
})
.error(function(data, status, headers, config) {
// handle error things
})
};

unable to fetch json data from $http post method in angularjs

I am unable to fetch json data from $http post method in angularjs
I had try following code in my controller file
var sectionTypeVM = new Object();
sectionTypeVM.sessionid = 'Session';
sectionTypeVM.query = 'where LOB group by Report_Source_Type,Report_Object_Type';
// alert(JSON.stringify(sectionTypeVM));
$http({
method: 'post',
url: 'http://testserver/search',
dataType: 'json',
data: JSON.stringify(sectionTypeVM),
headers: { 'Content-Type': 'application/json' }
}).success(function(data, status, headers, config) {
alert('success');
alert(data);
})
.error(function(data, status, headers, config) {
alert('error');
alert(data+ status + headers + config );
});
My $http request return error function when called.
I am using AngularJS v1.2.16
Thanks
Gajanan

How do I repeat an ajax call in angular.js and stop it based on response value

I have a simple controller that runs an ajax call. What is the most optimal running this ajax call every 15 seconds instead of just once. And how do I also stop the call?
myApp.controller('myCntrl', function($window,$scope,$http,$routeParams,$location) {
$http({
method: 'GET',
url: '../ajax/my_script.php',
})
.success(function(data, status, headers, config) {
console.log(data)
})
})
function httpCall (){
$http({
method: 'GET',
url: '../ajax/my_script.php',
})
.success(function(data, status, headers, config) {
if(data == "expectedData") //condition to stop recursive ajax.
$tiemout(httpCall, 15000);
})
}
You can use $interval service and stop it at an event as cancel(promise)
eg:
var promo = $interval(function(){
$http({
method: 'GET',
url: '../ajax/my_script.php',
})
.success(function(data, status, headers, config) {
console.log(data)
})
},1500);
clear interval as:
$interval.cancel(promo)
You could do it just by using $interval
var ajaxCall = function(){
$http({
method: 'GET',
url: '../ajax/my_script.php',
})
.success(function(data, status, headers, config) {
//on some condition
if(data.length == 0) //just for demonstration..you could add your own condition.
$interval.cancel(interval); //this is to cancel interval
console.log(data)
})
}
var interval = $interval(ajaxCall,15000)

ajax callback collision in asynchronous mode

$http({
url: "php/load.php",
method: "GET",
params: {'userId':userId}
}).success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
});
$scope.data[0]= "something";
How to wait above ajax to finish loading then load $scope.data[0]= "something";? I tried to place it within the $http and below $scope.data=data but it seem to have collision still..
var promise1 = $http({
url: "php/load.php",
method: "GET",
async: false,
params: {'userId':userId}
});
$.when(promise1).then(
function(data, status, headers, config) {
$scope.data = data;
},
function(data, status, headers, config) {
}
).done(function() {
$scope.data[0]= "something";
});
Haven't tested, but that should be right. Look into jQuery.when() and jQuery.promise() if not.

ajax and php error : Undefined index: userid in my case

where have I went wrong?
$http({
url: "php/loaduser.php?userid=user_id",
method: "GET",
data: {'user_id':'1'}
}).success(function(data, status, headers, config) {
mydata = [];
mydata = data;
console.log(mydata);
}).error(function(data, status, headers, config) {
// $scope.status = status;
alert(status);
});
my php
echo $_GET['user_id'];
it return Undefined index: user_id
Your call should be like
$http({
url: "php/loaduser.php",
method: "GET",
data: {'user_id':'1'}
})
Or either you can use
$http({
url: "php/loaduser.php?user_id=" + user_id,
method: "GET"
})
In second Method you are directly passing the user_id through the url whereas in the first you are sending it in a variable manner
You are sending in the string user_id.
url: "php/loaduser.php?userid=user_id"
Remove that last query parameter.
Change it to this:
url: "php/loaduser.php"
try this--
$.ajax({
type:"GET",
url:"php/loaduser.php",
data: {'user_id':1}
success:function(data, status, headers, config){
ydata = [];
mydata = data;
console.log(mydata);
}),
error(function(data, status, headers, config) {
// $scope.status = status;
alert(status);
}
});
The key in your URL is wrong: you need to change it from userid to user_id.
Then, you don't need to set the data property, you can include the parameter in your URL like so:
url: "php/loaduser.php?user_id=" + '1'

Categories