I try to send data from a HTML form to a REST API which is created with the Symfony 2 PHP framework. The form data is processed with javascript (AngularJS) and send as JSON string to the API. Sending the data as HTTP PUT request works well. The problem appears if I try to validate the data.
To validate the data I created a form class and use the form validation mechanism of symfony2. Here the problem appears. The validation mechanism does not recognize my date format ... I tried it with the following format :
{'year':'2014','month':'05','day':'15'}
and
2014-05-15
In both cases the validation throws the error "birthday:\n ERROR: This value is not valid.\n"
I would be very pleased if someone could give me a hint to solve this.
Thanks in advance
Related
I want to programmatically do HTTP post by parsing the below URL.
I am parsing the HTML Form parameters and then trying to post but it seems there is another parameter being added in runtime called metadata1 which is not there in the form.
Can someone help me how to figure out metadata1 ?
https://www.amazon.in/ap/signin?_encoding=UTF8&openid.assoc_handle=inflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.in%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26action%3Dsign-out%26path%3D%252Fgp%252Fyourstore%252Fhome%26ref_%3Dnav__gno_signout%26signIn%3D1%26useRedirectOnSuccess%3D1
I am working on adding a new feature to an existing Classic ASP application. In this feature the user will scan a bar-code which will input its value into a text field in a form within a bootstrap modal. The scanner is configured to send a TAB as well after the data.
What I need is a way to query the database on field exit and populate other fields in the modal with data from the query that is based on the scanned value. I need to do this without reloading the page or closing the modal so the user can verify this information and make changes before saving the form.
What is the best way to do this? I have no issue writing the SP, but do not have any idea how to call it and then return it's values. Can I possibly use JavaScript/PHP for this?
You will need to use some ajax
https://learn.jquery.com/ajax/
Basically you would send a request to your .asp script which will run the stored procedure then send a response using Response.Write.
Then parse that response in jquery.
Unlike ASP Classic, AJAX is a new technology so it works better with JSON.
There aren't many JSON libraries
You can use json2.asp:
https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json2.asp
Or if you want you can try it XML, but its unpopular these days and more tedious to parse.
You mentionned PHP, that's not a bad idea because, it's all built in for you see:
http://php.net/manual/en/function.json-encode.php
Hope this helps
I'm currently working on a Symfony2 project where a user can comment on parts of a webpage via contenteditable spans. I now want to save these comments, that are assembled in an array of JSON objects, into the database, with a reference to the page and the user id. This is where I'm stuck.
What steps are needed to put the data from the JavaScript code into the specific DB table?
I already created a Comment class in the Entity folder. And based on this
answer I added the following code in my saveComment() in JavaScript:
$.post('saveComments.php', {
page_id : getPageId(),
user_id : getUserId(),
comments : getJSONcomments(),
});
What next...?
Ok, let's create a saveComments.php ... but where in the bundle? Or could this be done with a Controller? If so, how and what would I need to replace the url ("saveComments.php") in the $.post(...) call with?
Don't post to a PHP page as this breaks the Symfony 2 model. Instead post to a route, like save/comment or along these lines.
Configure the route to point to an action in a controller.
Implement the controller and the action so that it does not take any parameters. Inside the action unpack the posted data the usual PHP way using (because you won't have a form to bind your data to):
$_POST
Just to get it working, echo a var_dump() to the console and see what you get. This is an example on how to write to the console.
Decode the JSON data with the serializer.
The decoded data will be a simple associative PHP array. Interpret the data you received and act accordingly (don't forget to handle security and all that stuff, too -- you don't want to open up security holes through AJAX).
Best is, you check that the route you chose falls into the security tier you need and probably already have configured in the Symfony 2 application. This way you don't need to handle security manually in the action.
Once this is done return an HTTP response with code 200 OK.
return new Response('', Response::HTTP_OK); // no response text at all
If there's any error reply with a 500 class HTTP server error:
return new Response('', Response::HTTP_INTERNAL_SERVER_ERROR); // 500
Also, don't forget to handle client-side errors with the .error() method.
I am working on a webapp. I have a webpage that sends some data to the server via form tag. The server returns a json array. I want to know how to parse that json array without loading
my page. The purpose is to keep my webpage as it is and load some values from json array to
some text fields below my form tag for further processing.
edit: I have tried some examples for reading from json array but how to prevent webpage realoading.
Please use Ajax for this. Ajax calls are done using Javascript as per your requirement. If you are using library like JQuery it is quite simple to use.
Link to JQuery - Get: http://api.jquery.com/jQuery.get/
One of the ideas to save you from changing anything in your existing code is handle onsubmit of the form. Send your get/post on submit via ajax and cancel the submit of the form.
I'm building REST web app and my server side Java code expects request body to have pure JSON string.
My goal is to use regular HTTP POST method (not ajax) and set JSON into request body message.
If I use ajax, it is very simple. Something like:
var jsonInput = '{"foo":"bar"}';
ajaxRequest.setRequestHeader("Content-Type", "application/json");
ajaxRequest.send(jsonInput)
But I want to use regular HTTP POST.
Looking at the thread below, the answer is to create a form with hidden input field and put JSON in to the input field and have server side code handle the rest.
JavaScript post request like a form submit
I tried that and it works just fine but do I really have to bother creating new form and input field to accomplish this? Or is there any other way available?
NOTE: I don't want to use JQuery or Prototype framework. Just simple Javascript.
FORMs only allow for name-value pairs to be submitted, and so your JSON needs to be inserted into one such pair.
Yout can read about this at w3.org
As a solution, what about using xhr to send the data to the server, exchange it for some token, and then direct the user via a regular GET to a url passing the token?
This should fit with the REST paradigm too.