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().
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 tried to make a GET call from a self written API in javascript (JQuery). I get an error but there is no message included.
Client code looks like this
$(document).ready(function () {
$.ajax({
type: "GET",
url: "APIURL...",
headers: {"Content-Type": "application/json;odata=verbose", "Accept": "application/json;odata=verbose", "crossDomain": "true", "credentials":"include"},
success: function (data) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus); //output: error
console.log(errorThrown); //output:
}
});
});
Also tested this with another url of a REST Test API https://jsonplaceholder.typicode.com/todos/1. This works fine, it outputs the expected data in the success function. So I think the problem is somewhere in my own API.
I made the API with PHP and the Slim Framework. Code looks like this
$app->get ( '/test', function ($request, $response) {
$data["test"] = "data";
$json = json_encode ( $data );
$response->write ( $json );
return $response;
});
This is the called function from my jquery code. In the browser its output is as expected {"test":"data"}
The server where my API runs is reachable from the internet. Any solutions or suggestions?
I'm making a adressbook app via laravel 5.2 and vuejs, my app needs CRUD functionality, i stuck at Update part, i send the data via ajax to laravel
and i get the data in laravel but i cant update rows.
this is my method in vuejs that handle updating:
updatecontact:function(){
var contactid = this.editingcontact.id;
var contact update = JSON.stringify(this.editingcontact);
this.$http({url: '/adressbook/'+contactid, data: {contactupdate} , method: 'PATCH'})
.then(function (response) {
console.log(response);
}, function (response) {
// error callback
});
and this the method that handles ajax request in laravel(it's a PUT)
public function update(Request $request, $id)
{
$adressbook = Adressbook::findorFail($id);
$adressbook->save($request->all());
}
at last this is how the data looks like:
contactupdate: "{"id":5,"companyName":"poolad","zamineKar":"test","tel":"44044440","fax":"44044422","email"}"
A better way to do it would just be to send this.editingcontact as the data:
updatecontact:function(){
var contactid = this.editingcontact.id;
this.$http({url: '/adressbook/'+contactid, data: this.editingcontact , method: 'PATCH'})
.then(function (response) {
console.log(response);
}, function (response) {
// error callback
});
Then this update code should work:
public function update(Request $request, $id)
{
$adressbook = Adressbook::findOrFail($id);
$adressbook->update($request->all());
}
I have looked everywhere, I found, that FUELPHP not handle Ajax requests, native and easily, as does RubyOnRails for example.
There must be done manually through jquery, unless I'm missing something I see is this: you have to use preventDefault () for the submit event of the form to create a post, product or whatever, and use the $ function post () to send the relevant parameters, which I think is ridiculous for a framework that claims to be based on the best ideas from other frameworks.
Please tell me if I'm wrong, I like FUELPHP, and I'm thinking about choosing it as PHP framework, but I want to be clear about this.
why not you can handle ajax in fuelphp like this.
.01. create ajax request in your view or public/assets/ create javascript file,
<script> $('.login').on('click',function(){
$.ajax({
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (e) {
alert("loading");
}, false);
return xhr;
},
url: "<?php echo \Uri::create('auth/login'); ?>",
type: "POST",
dataType: 'json'
data: {'username':$('#username').val(), 'password':$('#password').val()},
success: function (data) {
alert(data.status);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error");
}
});
});
</script>
.02. after that you can handle post data in classes/controller/ create auth.php and login method like this,
<?php
class Controller_Auth extends \Controller_Template {
function action_login() {
if (\Input::is_ajax()) {
if (\Input::param('username') and \Input::param('password')) {
$username = \Input::param('username');
$password = md5(\Input::param('password'));
//check password and username or anything
$msg = 'fail';
return json_encode(array('status' => $msg));
}
}
...
}
}
?>
you can handle data like this. i think you got something. and this is helpful.
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.