Create couchdb doc using AngularJs - angularjs

var post_data ={ "task_list": $scope.task_list,"uri": $scope.uri }
$http({method: 'POST',url: 'http://127.0.0.1:5984/tasklist/'+$scope.task_list, data: post_data})
.success(function(data, status, headers, config) {
console.log("POST SUCCESS")
alert("Successfully added");
})
.error(function(data, status, headers, config) {
console.log("POST ERROR", data)
})
}
} );
When I compile the above code it shows an error of
127.0.0.1:5984/tasklist/text:1 POST http://127.0.0.1:5984/tasklist/text 400 (Bad Request)
admin.html:63 POST ERROR Object {error: "bad_request", reason: "Referer header required."}
Why does it show this? Any remedy for this?

Hey you need include headers in the post data. Since your post url expecting Referer.
var post_data = {
method: 'POST',
url: 'http://127.0.0.1:5984/tasklist/'+$scope.task_list,
headers: {
'Content-Type': 'json',
'Referer': 'http://www.example.com/app/index.html' //include headers
},
data: { "task_list": $scope.task_list,"uri": $scope.uri }
}
$http(post_data).success(function(data, status, headers, config) {
console.log("POST SUCCESS")
alert("Successfully added");
})
.error(function(data, status, headers, config) {
console.log("POST ERROR", data)
});

Related

Angular is waiting anoher request to start

In my App I need to do a request to save some data (this request is little slow) but the user can do another request in same time, when he did this, angular was waiting for the first request (not assync). How Can I do two requests whithout waiting for other?
My slow service:
app.service('PedSaveService',["$http", "$q","BASEURL",
function ($http, $q,BASEURL) {
var self = this;
self.Save = function (pedidoData) {
var deferred = $q.defer();
$http({method: 'POST',async: true, url: BASEURL.REST_SAVE, headers: {'Content-Type': 'application/json;charset=utf-8'}, data : pedidoData})
.success(function (response, status, headers, config) {
deferred.resolve(response, status, headers, config);
})
.error(function (response, status, headers, config) {
deferred.reject(response, status, headers, config);
});
return deferred.promise;
}
self.Save = function(pedidoData) {
$http({
method: 'POST',
async: true,
url: BASEURL.REST_SAVE,
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
data: pedidoData
}).then(function(res) {
return res;
}); }
Your code in service should be this.

Use filterFilter to filter data

I want to filter retrived sharepoint list using filterFilter.But it is giving ***error as:**angular.min.js:89 TypeError: filterFilter is not a function*****
Not able to inject filter to controller since am using $https.
Below is the code,
$scope.update=function()
{
$scope.drop=$scope.states.Id;
console.log($scope.drop);
$http({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getByTitle('List')/items?$select=State/Title,Title,Id&$expand=State/Title",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" }
})
.success(function (data, status, headers, config) {
$scope.city= filterFilter(data.d.results,{State:$scope.drop});
console.log($scope.city);
})
.error (function(data, status, headers, config) { })
}
Thank you for your reply ,i got my answer,Complete code is below
app.controller('Ctrl', function($filter,$scope,$http,filterFilter)
{
$http({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getByTitle('States')/items",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" }
})
.success(function (data, status, headers, config) {
$scope.State=data.d.results;
console.log($scope.State);
})
.error (function(data, status, headers, config) { })
$scope.update=function()
{
$scope.drop=$scope.states.Id;
console.log($scope.drop);
$http({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getByTitle('City')/items?$select=State/Title,Title,Id,*&$expand=State",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" }
})
.success(function (data, status, headers, config) {
$scope.city=filterFilter(data.d.results,{StateId:$scope.drop});
console.log($scope.city);
})
.error (function(data, status, headers, config) { })
}
});
If you use * in $select,Lookup field Id can be retrieved,using that you can compare :)

Angular $http is not sending data

I know it has been solved here many times, but I'm still not able to get it working.
My js call is:
var data = { value: 7 };
$http.post('api/controller/method', data);
But in fiddler there is no Content-Type and no JSON data.
I want the Content-Type to be 'application/json' which should be default, right?
Thanks
var data = { value: 7 };
$http({
url: "api/controller/method",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param(data)
}).success(function(data, status, headers, config) {
//some code when success post
}).error(function(data, status, headers, config) {
//some code when error post
});
Try this:
var response = $http.post('api/controller/method', data);
response.success(function(data, status1, headers, config) {
//
}

Making post request to server through angularjs getting error 404

.controller("HttpPostController", function ($scope, $http)
{
console.log('SendHttpPostData login', $scope.loginData);
$scope.SendHttpPostData = function ()
{
var data =
{
name:$scope.loginData.username,
pwd:$scope.loginData.password
}
var config =
{
headers : {
'Content-Type': 'application/json;charset=utf-8;'
}
}
$http.post('demo url', data, config)
.success(function (data, status, headers, config)
{
console.log(" success"+status);
})
.error(function (data, status, header, config)
{
console.log(" error"+status);
});
};
This is my code , I have called function SendHttpPostData() on
ng-submit="SendHttpPostData()" in html file and added ng-controller in div properly than to i am getting 404 error whenever SendHttpPostData() is called,Please help me.
Try this if it may help you. But you haven't defined demo_url.
var demo_url = 'http://facebook.com:8081';//whatever your url is
$http({
method: 'POST', url: demo_url, data: { name:$scope.loginData.username, password: pwd:$scope.loginData.password },
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
}).success(function (data, status, headers, config)
{
console.log(" success"+status);
})
.error(function (data, status, header, config)
{
console.log(" error"+status);
});

AngularJS a service is not a function

This current code when ran returns TypeError: Services.updateUserData.set is not a function
Controller
Services.updateUserData.set($rootScope.user);
Services.js
this.updateUserData = function() {
return {
set: function(data) {
console.log(data);
$http({
method: 'POST',
url: 'api/data/',
data: {'data': data},
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
}).success(function(data, status, headers, config) {
console.log(data);
}).error(function(data, status, headers, config) {
console.log(data);
});
}
}
}
The code below works but I would much rather have a get and set function within updateUserData so that I am not duplicating work.
Current Code
Controller
Services.updateUserData($rootScope.user);
Services.js
this.updateUserData = function() {
console.log(data);
$http({
method: 'POST',
url: 'api/data/',
data: {'data': data},
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
}).success(function(data, status, headers, config) {
console.log(data);
}).error(function(data, status, headers, config) {
console.log(data);
});
}
updateUserData is a function and hence there is no property called set. The option is either convert it to object
this.updateUserData={set:function(){}}
or invoke it
Services.updateUserData().set($rootScope.user);
before accessing set

Resources