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'
Related
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
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)
$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.
$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
The following $http request executes successfully, yet the PHP script on the other end receives an empty $_POST array when it should receive 'test' and 'testval.' Any ideas?
$http({
url: 'backend.php',
method: "POST",
data: {'test': 'testval'},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data, status, headers, config) {
console.log(data);
}).error(function (data, status, headers, config) {});
If you wan to send just that simple data, try this:
$http({
url: 'backend.php',
method: "POST",
data: 'test=' + testval,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data, status, headers, config) {
console.log(data);
}).error(function (data, status, headers, config) {});
And php part shoul be like this:
<?php
$data = $_POST['test'];
$echo $data;
?>
It is working for me.
This is a common issue with AngularJS.
The first step is to change the default content-type header for POST request:
$http.defaults.headers.post["Content-Type"] =
"application/x-www-form-urlencoded; charset=UTF-8;";
Then, using an XHR request interceptor, it is necessary to serialize properly the payload object:
$httpProvider.interceptors.push(['$q', function($q) {
return {
request: function(config) {
if (config.data && typeof config.data === 'object') {
// Check https://gist.github.com/brunoscopelliti/7492579
// for a possible way to implement the serialize function.
config.data = serialize(config.data);
}
return config || $q.when(config);
}
};
}]);
In this way, payload data will be again available in the $_POST array.
For more info about XHR interceptor.
Another possibility, it is to mantain the default content-type header, and then server side parse the payload:
if(stripos($_SERVER["CONTENT_TYPE"], "application/json") === 0) {
$_POST = json_decode(file_get_contents("php://input"), true);
}
More simplified way:
myApp.config(function($httpProvider) {
$httpProvider.defaults.transformRequest = function(data) {
if (data === undefined) { return data; }
return $.param(data);
};
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
});
Remove the following line, and the preceding comma:
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
And then the data will appear in $_POST. You only need that line if you are uploading a file, in which case you'll have to decode the body to get the data vars.
I found my solution here http://www.peterbe.com/plog/what-stumped-me-about-angularjs. There is a piece of code in the "AJAX doesn't work like jQuery" section, which solved my problem.