As part of the service response there is some content received in local language. The response from the server looks fine, but the moment it's received through
angular.js $http, the content in local language is distorted.
I've tried both the methods given below, but no luck. Can someone help please?
Method 1:
appService.config(function($httpProvider) {
$httpProvider.defaults.headers.get = { 'Content-Type' : 'application/json;charset=utf-8' };
});
$http.get(url).success(function(data, status, headers, config) {
console.log('data: ' + data);
}).error(function(data, status, headers, config) {
console.log('data: ' + data);
});
Method 2:
$http.get(url,{ transformRequest: angular.identity,
headers: {'Content-Type': 'application/json;charset=utf-8'}}
).success(function(data, status, headers, config) {
console.log('data: ' + data);
}).error(function(data, status, headers, config) {
console.log('data: ' + data);
});
Angular response content looks something like this �����
Related
I am using grails 3.x as the restful service provider. A simple rest resource is provided like:
#Resource(uri ='/users', readOnly = false, formats = ['json', 'xml'])
class User {
String username
String email
String password
}
I've tested it by curl successfully like :
$ curl -H "Content-Type: application/json" -X POST -d "{\"username\":\"xyz\",\"password\":\"xyz\", \"email\":\"w#g.com\"}" http://localhost:8080/users
{"id":1,"email":"w#g.com","password":"xyz","username":"xyz"}
but when I use angularjs to perform the http post ajax call,
$http.post(url, {"username":"dummy", "password":"test", "email":"email#g.com"})
.success(function(data, status, headers, config) {
// $location.path('/listUsers');
console.log("data " + data);
}).error(function(data, status, headers, config) {
console.log("error occured... " + status);
});
it always throw the 422 status even with the same exact values.
You can place this header globally as follows -
angular.module('myApp', [])
.config(function ($httpProvider) {
$httpProvider.defaults.headers.put['Content-Type'] = 'application/json';
$httpProvider.defaults.headers.post['Content-Type'] = 'application/json';
})
Finally I figured it out. Thank you for all your comments.
var config = {
'method': 'POST',
'url': "http://localhost:8080/users",
'headers': {
'Content-Type': 'application/json'
},
'data': {"username":"dummy", "password":"test", "email":"email#g.com"}
}
$http(config)
.success(function(data, status, headers, config) {
// $location.path('/listUsers');
console.log("data " + data);
}).error(function(data, status, headers, config) {
console.log("error occured... " + status);
});
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) {
//
}
I'm trying to upload file to Dropbox with following code. File is successfully uploaded to Dropbox but size gone 0 bytes.
I'm now planning to upload microsoft word and pdf file.
$scope.uploadHtmlFile = function($files) {
var data = $files;
$http({
method: 'PUT',
url: 'https://api-content.dropbox.com/1/files_put/dropbox/' + $files[0].name + '?access_token=TOKEN',
data: data
}).success(function(data, status, headers, config) {
console.log(data);
console.log('file uploaded successfully');
}).error(function(data, status, headers, config) {
console.log('error : ' + data);
console.log('erro file uploaded successfully');
});
}
$files is an array object, and file will be referred using $files[0].
$scope.uploadHtmlFile = function($files) {
var data = $files[0];
$http({
method: 'PUT',
url: 'https://api-content.dropbox.com/1/files_put/dropbox/' + $files[0].name + '?access_token=TOKEN',
data: data
}).success(function(data, status, headers, config) {
console.log(data);
console.log('file uploaded successfully');
}).error(function(data, status, headers, config) {
console.log('error : ' + data);
console.log('erro file uploaded successfully');
});
}
I am making an angular http post to an API which like this
$http({
method: 'POST',
url: 'http://api/ClientEndpoint',
data: register,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
}).error(function (data, status, headers, config) {
alert(data);
});
which is returning the following response
But I am not able to store the response path in a variable. How can I do that?
It is cross domain scenario and this is the response being showed in chrome console
I want to access store the ClientEndpoint value
Did you forget the .success(function (data) { }) callback ? just like the .error() you put after the http() function
i hope this will solve your problem ;)
Use the headers variable in either your .succes(response, headers) or .error(response, headers).
E.g :
.success(function(data, status, headers, config) {
//Success Handling
});
.error(function(data, status, headers, config) {
//Error Handeling
});
This only works if you are on the same domain as the server, if it's cross domain, the server has to send the Access-Control-Expose-Headers for you to make this work.
I am using the following:
var url = '/api/Test/Mark';
$http('POST', url, $scope.content.answers)
.success(function (data, status, headers, config) {
$scope.content = data.text;
})
.error(function (data, status, headers, config) {
alert("Error: No data returned from " + url);
});
This gives me an error when I try to call it:
TypeError: Cannot use 'in' operator to search for '3' in POST
at isArrayLike (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:183:81)
at forEach (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:225:16)
at http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:329:7
at forEach (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:227:18)
at extend (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:327:3)
at $http (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:6256:7)
at Scope.$scope.markQuestion (http://127.0.0.1:81/Content/app/questions/controllers/content.js:39:13)
at elementFns (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:8564:19)
at http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:15511:13
at Scope.$get.Scope.$eval (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:10034:28)
Can someone give me advice on what I may be doing wrong? Note $scope.content.answers is an array of data.
You are doing wrong with $http
Try This
var url = '/api/Test/Mark';
$http({
method: 'POST',
url: url
data: $scope.content.answers // this should be a object e.g { a : 'one', b: 'Two' }
})
.success(function (data, status, headers, config) {
$scope.content = data.text;
})
.error(function (data, status, headers, config) {
alert("Error: No data returned from " + url);
});