I'm working with a Java backend with Spring MVC framework, I have a service that takes a List and remove some objects in a database. When I use Postman I send the next JSON object:
["ce8249aa-1ede-40b9-a158-d2c417c23df7",
"73a629b9-bae8-44aa-83c3-e8ee0fc96325",
"50c45e52-2c74-40ec-93e7-1b5379eae5db",
"c8a61e92-bc6d-47d0-a3e2-bda9ad85cecc"]
Then I used a service in Angularjs sending this object:
$scope.accounts = new Array("ce8249aa-1ede-40b9-a158-d2c417c23df7",
"73a629b9-bae8-44aa-83c3-e8ee0fc96325",
"50c45e52-2c74-40ec-93e7-1b5379eae5db",
"c8a61e92-bc6d-47d0-a3e2-bda9ad85cecc");
But I got this error:
The request sent by the client was syntactically incorrect.
This problem does not occurs with other JSON objects, for example:
From Postman:
{
"accountName":"XxxxxXXxxxx",
"paymentMethodMain":"Medio Pago",
"accountType":"xx",
"accountNumber":"123456AA"
}
And from Angular:
$scope.account = {
"accountName":"XxxxxXXxxxx",
"paymentMethodMain":"Medio Pago",
"accountType":"xx",
"accountNumber":"123456AA"
};
In this case all works correctly.
I think you can't post an array like this. Either you have to send the array as string/text in request body and parse in server side or do something like
$scope.accounts = new Array("ce8249aa-1ede-40b9-a158-d2c417c23df7",
"73a629b9-bae8-44aa-83c3-e8ee0fc96325",
"50c45e52-2c74-40ec-93e7-1b5379eae5db",
"c8a61e92-bc6d-47d0-a3e2-bda9ad85cecc");
// in your request send the array as value to a json key
{data: $scope.accounts }
in your server try
request.getParameterValues("data[]");
Related
In my Maven app I have mapped put http request onto this function(using spring framework), and I want to check something inside it and send response as text. Then I want to send that request from angularjs and store that response into some variable from angularjs controller. This is what I have tried.
#RequestMapping(path="/play", method={RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public String someFunction(){
//...
return "some text";
}
$scope.getResponse = function(param1, param2...){
$http.post("url..").then(
function(response){
$scope.response = response.data.response;
console.info('success');
},
function(response){
console.info('failure');
})
}
Http is mapped correctly and works from browser, problem is how to store textual response into some angularjs variable from controller.
It seems $http finds it difficult to parse the invalid JSON data in the response.
We have produces = {MediaType.APPLICATION_JSON_UTF8_VALUE} in there for the API and sending out plain text. That's why it goes to the failure handler.
Change the media type to MediaType.TEXT_PLAIN_VALUE and see if it works...
I am working on a project using Parse.com rest api and Ionic/angularjs.
When I issue a Post request, I would like to get the objectId from the Response body.
I can see that the objectId is included in the response in json format, but I canĀ“t seem to extract it.
var signUp = new SignUp(signUpData);
var response = signUp.$save(); // using angularjs $resource
console.log(response);
When I log the reponse I get this in the console: Object { $$state: Object }
"Dotting" into $$state only returns a number.
Any help would be appreciated.
I never used Parse.com but if you are using $resource code below should works
var signUp = new SignUp(signUpData);
signUp.$save(function(response){
console.log(response);
});
I tried to use Angular $http.post(url, data) method. But I am facing some problems to post correct data to server (WebAPI). I have tried follwing options but none is working.
var data1 = new Object();
data1.UserName = "UserA1";
data1.Password = "password123";
data1.ConfirmPassword = "password123";
var data2 = angular.toJson(data1);
var data3 = JSON.stringify(data1);
var data4 = { UserName: "UserA2", Password: "password123", ConfirmPassword: "password123" };
var data5 = "UserName=UserA3&Password=password123&ConfirmPassword=password123";
$http.post(url, data1)
$http.post(url, data2)
$http.post(url, data3)
$http.post(url, data4)
$http.post(url, data5)
None is working. What kind of data is correct for this method. I tried JQuery.post() with above data and it works. Its super strange for me why this simple angularjs method so hard to use or I am missing something.
Update:
Basically I am working on following example. I want to register user from client side (/api/Account/Register).
http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api
This is because your server-side expects a request with its content x-www-form-urlencoded.
jQuery's $.post() sends the request with a Content-type of application/x-www-form-urlencoded whereas Angular's $http.post() sends the request with a Content-type of application/json (and also encodes the data as JSON (instad of form-data) if a JS Object is passed).
There are methods you can send x-www-form-urlencoded requests using $http (and there are several related answers on SO), but it involves less straight-forward code.
I suggest you change your server side to consume JSON requests.
Depending on what you have on server side, you should transform your request (that's the case for PHP, for example, or you can read from the input stream: php://input).
See this gist: https://gist.github.com/JensRantil/5713606.
If this is not solving your problem, please inspect the requests angular is creating (using the Developer Tools -> Network tab in Chrome for example), and tell us what you see.
I am facing very strange issue here. I have a servlet running on my machine which renders my web page based on some input parameters.
Now, my screen capture with PhantomJS is not working if I try to send my data as a JSON object as a POST request type. For e.g. if I try:
Client side
var data = {"op": "get"};
page.open(address, 'POST', data, function (status) {
if (status !== 'success') {
console.log('[ERROR] :: Unable to fetch the address - ' + address + ", with data - " + data);
phantom.exit();
} else {
page.render(output);
}
console.log('processing...');
});
Server side
Now, on the server side, I am using Apache Velocity View templating so I have a single method which handles both get and post like :
public Template handleRequest(HttpServletRequest request, HttpServletResponse response,
Context context){
System.out.println(request.getParameter("op"));
//Always null
}
However, if I try sending my data from phantomjs as:
var data = "op=get&..."
It works
Also, at many places elsewhere in my code..I am doing Ajax POST requests to the same servlet and it works perfectly for all those request.
Would anybody explain why my servlet is not reading the JSON parameters passed from Phantomjs?
Servlets deal with simple request, so they only know how to parse (natively) HTTP parameters, either GET parameters from the URL, or POST parameters sent as application/x-www-form-urlencoded. Newer versions of the Servlet specification can also read multipart/form-data.
But there's nothing about JSON mentioned either in the Servlet or the HTTP specifications. So you must use a library that knows how to parse JSON and make the resulting object available in the Velocity context.
I'm switching from jquery $.ajax, which was working fine, to using AngularJS $http.put to access a restful API.
I can make an API call, but the PUT data isn't getting sent - so my API sees a PUT request with an empty data object, which should contain a JSON string -> data.values = 'a json structure'
$http.put(
$rootScope.api_url,
{
values: jsonifiedValues
},
{
headers: {
apihash: sha256hash
}
}).success(function(data,status,headers,config){
// handle success
}).error(function(data,status,headers,config) {
// handle failure
});
I've not used AngularJS's $http before, but when I dump out the data in my PHP api it's just empty. this is how I'm pulling it from the request in the PHP:
parse_str(file_get_contents('php://input'), $put_vars);
$arr_req_data = $put_vars['values'];
In my API if the apihash sent from the request doesn't match the sha256 hash built on the PUT values, it fails.
This is working in JQuery, just failing now I've switched to $http. I'm not sure why the PUT data seems to be empty.
The return value from file_get_contents('php://input') will be a JSON string (provided everything got sent), so parse_str is not the right function to handle that data.
Instead use json_decode.
Also there is no need to send jsonified values, it will just make things more complicated as you'll have to use json_decode twice.