I sending a request to my php file to get data from database from my AngularJS file. Some-time I am getting data also lot of time there is no response is coming.
Here is my AngularJS code:
myApp.controller('PageCtrl', function PageCtrl($scope, $routeParams, $http) {
$http.post(APP_URL + "server/FullDesc.php", {id: parseInt($routeParams.Id)}).success(function (data) {
var timer = setTimeout(function () {
$scope.Details = data[0];
console.log(data);
$scope.$apply($scope.Details);
}, 10);
}, 'json');
})
In resposne I am getting output of my response like this
In image you can see there is no response showing. How to solve this issue.
You should try adding an error scenario as well:
myApp.controller('PageCtrl', function PageCtrl($scope, $routeParams, $http) {
$http.post(APP_URL + "server/FullDesc.php",
{id: parseInt($routeParams.Id)}
).then(function(successResponse){
var data = successResponse.data;
console.log('successResponse', data);
$scope.Details = data[0];
//$scope.$apply($scope.Details); // assigning to scope should automatically do an apply
}, function(errorResponse){
console.log('errorResponse', errorResponse);
});
});
Then you should also check what is being sent to the server and what the server sends back. It seems your server may be encountering an error and you are not sending an error response back.
Related
I'm trying to pass data to my laravel controller function via Ajax. At this point I just want to return the data being sent to verify ajax is working. I can "GET" with ajax, but when I try to "POST" ajax brakes.
Could someone please tell me what I'm doing wrong? Thank you.
Here is my ajax code...
var startMapLocation = { startCity: "Cleveland", startStat: "Oh" };
$.ajax({
type: "POST",
url: url,
data: startMapLocation,
success: function(data, status) {
//alert(data);
console.log("success:", data);
},
error: function() {
alert("Ajax Broke!" + status);
}
});
My laravel function is...
public function postphp( Request $request)
{
$a = $request->all();
$city = $a["startCity"];
return json_encode( $city );
}
Thanks every one for your help. To resolve this issue, I first had to verify that my route was a post route not a get route.
Route::post('/postphp', 'GSResultController#postphp');
I also need to get my csrf-token and add it to the ajax call.
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
},
This fixed my problem.
I'am working on angularjs project in which there are 2 users admin and office staff.And to insert data to database i need to send data to server using http request using post method and this request is received on server side in php. This is simple request to server with some parameters and its working correctly until both the users hit submit button and http request is made at the same time. ie when http request is made by both the users at the same time.If that happens than 2nd request is does not get any response.Now my question is that is there a way to check if server is processing one http request and make 2nd request wait.
angularjs:
$http({
method: "post",
url: $localStorage.weburl + "abc.php?f=abc_data",
timeout: 1 * 10 * 2000,
params: {
action: "add"
},
data: {
data1: data1,
data2: data2
}.then(function mySucces(response) {
}, function myError(err) {
$ionicLoading.hide();
alert("Please check the connection");
})
});
PHP:
function abc_data($conn)
{
$_POST = json_decode(file_get_contents('php://input'), true);
}
You should try like this way
var deferred = $q.defer();
$http.post($localStorage.weburl + "abc.php",{action: "add"})
.success(function (data) {
deferred.resolve(data);
})
.error(function (error) {
deferred.reject(error);
});
return deferred.promise;
If you are using post method then pass you data like {data:arrayOfdata} instead of url: $localStorage.weburl + "abc.php?f=abc_data"
Getting the error below in console and data is not fetching from mysql. Can anyone suggest on the error below:
Used angularjs, php, mysql and materializecss
Error: $http.get(...).success is not a function
Here is the code of my controller:
.controller('dashBoardCtrl', ['$scope', '$http', function ($scope, $http)
{
$http.get("dbconnection/db_read.php")
.success(function(data)
{
$scope.data = data;
console.log(data);
})
.error(function()
{
$scope.data = "error in fetching data";
});
}]
);
Can you try
$http.get().then(function(result){console.log(result)});
I believe .success is deprecated.
You're more than likely using AngularJS 1.6+
.success(fnSuccess) and .error(fnError) don't exist any more.
Use .then(fnSuccess, fnError) instead.
So
$http.get(...).success(function(){})
is now
$http.get(...).then(function success() {}, function error() {});
Let's look at the documentation:
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
So you pass your success and error callbacks to then() not success().
[UPDATED] Why does this piece of code keeps on falling to the error callback fxn, while also refreshing the page and submitting my form data to the server. When I also try to debug, it keeps on looping in the jquery file for so long, making me tired of pressing F11. :(
var ajax_load = {my image html};
var ajax_processor = 'http://localhost/patientcarev1/ajax_processor/save_physical_info';
$("#save2").click(function() {
var aData = $("#physical-info-form").serialize();
$("#ajax_load").html(ajax_load);
$("#ajax_load").html(ajax_load);
var request = $.ajax({
type: 'POST',
url: ajax_processor,
data: aData
});
request.done(function() { alert("success"); });
request.fail(function() { alert("error"); });
request.always(function() { alert("complete"); });
});
On step-by-step debug it is falling under the success callback fxn, but if not in debug mode it falls into to the fail callback.. this one is giving me a headache :(
So I am finding it difficult to post data to my php server. It seems to work from the crossrider end of things, but I am not sure what to use to collect the post with PHP.
Here is the crossrider code borrowed from the docs page:
appAPI.ready(function($) {
// Posting data using a JSON object
appAPI.request.post({
url: 'http://ultimarks.arcco96.c9.io/service.php',
// Data to post
postData: ["hello","world"],
onSuccess: function(response) {
//alert("Succeeded in posting data");
//alert(response);
},
onFailure: function(httpCode) {
//alert('Failed to retrieve content. (HTTP Code:' + httpCode + ')');
},
additionalRequestHeaders: {
myHeader: 'value'
},
contentType: 'application/json'
});
});
and this is my php code:
print_r($_post);
It returns Array() on both the php page and in an alert generated by crossrider (which is strange because I never programmed that).
Perhaps the problem is that I am trying to print something to a PHP page that's not a redirect from a form.
So how do I fix this problem?
Variables in PHP are case-sensitive. The correct PHP variable is $_POST not $_post
appAPI.ready(function($) {
var data1 = "Mydata1";
var data2 = "Mydata2";
appAPI.browserAction.onClick(function() {
appAPI.request.post({
url: 'http://www.example.com/Ext.php',
postData: 'data1='+ data1 +'&data2='+ data2,
onSuccess: function(response) {
alert("Succeeded in posting data");
//alert(response);
},
onFailure: function(httpCode) {
alert('in posting data. (HTTP Code:' + httpCode + ')');
},
additionalRequestHeaders: {
myHeader: 'value'
},
// contentType: 'application/json'
contentType: 'application/x-www-form-urlencoded'
});
}); });
Then, in the Ext.php file grab the post data using $_POST["data1"] and $_POST["data2"]
Sample code for $_POST[]:
http://www.w3schools.com/php/php_post.asp
PHP $_POST global variable does not get populated from JSON payload in requests. So, either you send data in x-www-form-urlencoded as retailcoder is suggesting, or modify the way your php scripts reads the input, as found here: POST JSON data via CURL and grabbing it