Error Using Method Post AngularJs - angularjs

can someone fix it, when i using postman my restfull is good but not from my client side
var req = {
method: 'POST',
url: 'http://localhost:8098/getfiles',
headers: {
'Content-Type': 'multipart/form-data',
},
data: { 'selectedFiles': 'D:\\quadtech\\Intellegent\\AppSpringBoot\\input\\bismillah.txt' }
}
return $http(req).then(function(response){console.log(response); return response;}, function(){});
error in my console is
POST http://localhost:8098/getfiles 500 ()
this is request from postman

Send using formData instead of data:
var req = {
method: 'POST',
url: 'http://localhost:8098/getfiles',
headers: {
'Content-Type': 'multipart/form-data',
},
formData: {
'selectedFiles': fs.createReadStream('D:\\quadtech\\Intellegent\\AppSpringBoot\\input\\bismillah.txt') }
}
return $http(req).then(function(response){console.log(response); return response;}, function(){});
see the docs: https://github.com/request/request#forms

Related

angularjs how can pass multipul parameter to api

How can I pass mutipul parameters from angularjs -to WebAPi My Api as its Required two parameters
public IHttpActionResult GetData(int pagesize,int Totalpages){---}
angularctrls.js
function GetData() {
var PageDetails={
'currentpage': $scope.currentPage,
'pagesize':$scope.pagesize
}
HomeFactory.GetDat(PageDetails).then(function () {
})
}
Factory.js
EmployeeServiceFactory.GetDat = function (PageDetails) {
return $http({
url:WebAPi+'Home/GetData/',
method: 'GET',
data: PageDetails,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
}
Getting Error as
Failed to load http://localhost:3084/Home/GetData/%7B%22currentpage%22:1,%22pagesize%22:5%7D: No 'Access-Control-Allow-Origin' header is present on the requested resource
try params instead of data like below:
EmployeeServiceFactory.GetDat = function (PageDetails) {
return $http({
url:WebAPi+'Home/GetData/',
method: 'GET',
params: PageDetails,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
}
I think you can put data as an object.
EmployeeServiceFactory.GetDat = function (PageDetails) {
return $http({
url:WebAPi+'Home/GetData/'+PageDetails,
method: 'GET',
data: {page_details:
PageDetails, foo: 'bar'}, headers: { 'Content-Type': 'application/x-www-form-urlencoded'
}
})
}

Angularjs http post request - params are not reaching to server

I am trying to create post request with angularjs. This is my code for adding an supplier to server.
SupplierService.addNewSupplier = function(supplier) {
alert (supplier.name);
alert (supplier.mobile);
var Indata = {'name': supplier.name, 'mobile': supplier.mobile};
//var Indata = {'product': $scope.product, 'product2': $scope.product2 };
var req = {
url: SupplierURL,
method: 'POST',
params: Indata,
headers: {'Content-Type': 'application/json'}
};
//$http.post(SupplierURL, Indata)
$http(req)
.then(function(success){
console.log(success);
//SupplierList.push({id: data, name: supplier.name, mobile: supplier.mobile});
supplier.name = "";
supplier.mobile = "";
},function (error){
console.log(error);
alert ('Supplier add error.');
});
};
After making the request, callback is coming to error part and in console log i can see.
{error: true, message: "Failed to create supplier. Please try again", name: null, mobile: null}
Why name and mobile is not reaching to server. Same request works fine with postman(api testing tool). So i don't thing there can be anu issue in server side code. Any help will be appreciated.
From the $http documentation you can clearly see request parameters:
https://docs.angularjs.org/api/ng/service/$http
var req = {
method: 'POST',
url: 'http://example.com',
headers: {
'Content-Type': undefined
},
data: { test: 'test' }
}
$http(req).then(function(){...}, function(){...});
So, in your case you need to switch params with data:
var req = {
url: SupplierURL,
method: 'POST',
data: Indata,
headers: {'Content-Type': 'application/json'}
};

when i try to posting data from angular to server Why data is not Bindig at serverside

Here im trying to posting data from angular to api server in angular side it maping properly but wht it not binding at serverside
Emplyeectr.js
Emplyeeserve.js
this.saveEmp = function (savingdata) {
var sersave = $http({
url: Privateurl2+'SaveEmpData',
method: "POST",
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: JSON.stringify(savingdata),
content: { 'content-type': 'application/Json' }
})
return sersave;
}
Emplyeectrl.js
$scope.SaveDb = function (user) {
if ($scope.Validate = "UpdateEmpDetails")
$scope.Submitted = true;
if ($scope.isFormValid) {
var savingdata = {
EmpName: $scope.User.EmpName,
Gen_Name: $scope.User.Gen_Name,
Email: $scope.User.Email,
Psw: $scope.User.Psw,
Cnt_Id: $scope.User.Cnt_Id,
Sts_Id: $scope.User.Sts_Id,
City_Id: $scope.User.City_Id,
}
var saving = Myservice.saveEmp(savingdata);
saving.then(function () {
})
}
}
When i trace it from Fiddler it passing data as
{"EmpName":"Aa1.com","Gen_Name":"Male","Email":"Das#gmail","Psw":"1478","Cnt_Id":2,"Sts_Id":4,"City_Id":7}
The content you are posting is not in the correct format. You are trying to post JSON but telling the server it's form data.
You should change
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
to
headers: { 'Content-Type': 'application/json' },
you should probably remove the content property as it's not in the AngularJS docs and probably a little mis-leading.

angular ionic $http sends null data to rest API

I am calling a rest API using
method is "POST"
requestData is
{
'username': user.userName,
'password': user.password,
'deviceID': deviceid,
'latlng': null,
'pincode': null
}
var req = {
method: method,
url: 'http://url/' + Api,
headers: {
'Token': 'Basic bUY5VkV0eHBOK3JUYUF1TGJjM1FHRGh4N1hYejhYSEw=',
'Content-Type': 'application/json'
},
data: {
'contentType': 'application/json',
'content': requestData
},
}
console.log(req);
$http(req).then(successCallback, errorCallback);
My rest APA is
#RequestMapping(value = "/authenticateUser", method = RequestMethod.POST, consumes = "application/json")
public AuthenticateUserBean authenticateUser(#RequestBody final AuthenticateUserBean authenticateUserBean) {
I am receiving null values for all the data such as username, password.
It is an ionic hybrid app with angular js.
I am calling it using chrome.
Cors is already disabled.
Your request format has few issues:
No need of contentType in data
You can directly send your requestData as data
Try with below code:
var req = {
method: method,
url: 'http://url/' + Api,
dataType: 'json',
headers: {
'Token': 'Basic bUY5VkV0eHBOK3JUYUF1TGJjM1FHRGh4N1hYejhYSEw=',
'Content-Type': 'application/json'
},
data: requestData,
}
console.log(req);
$http(req).then(successCallback, errorCallback);

Post data from angular to node js route not working

I am trying to send array subjectAverage to nodejs route. I am using express route, its showing unidentified when trying to print on console.
var app = angular.module("myapp", [])
app.controller("ListController2", ['$scope','getAverage','$http',function($scope, getAverage,$http) {
$scope.subjectAverages = [{
'subjectName': '',
'yearSem': '',
'studentsAppeared': '',
'studentsPassed':'',
'percentage':'',
'average':'',
'facultyComment':''
}];
$scope.save=function(){
var data =angular.toJson($scope.subjectAverages)
var objectToSerialize={'object':data};
$http({
url: 'app',
method: "POST",
data: $.param(objectToSerialize),
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).success(function(data){
alert("done");
});
}
router.post('/app',function(res,req){
console.log(req.body);
});
Please guide.
Try this :
$scope.save=function(){
var data = {object : $scope.subjectAverages};
$http({
url: '/app',
method: "POST",
data: data,
headers: {
'Content-Type': 'application/json'
}
}).then(function(data){ //.success is deprecated,so use .then
alert("done");
})
.catch(function(err){//using .catch instead of .error as it is deprecated
console.log("Error in request =>", err)
});

Resources