jQuery Select Option from Json

jQuery select option population using json as data source

jQuery select option tutorial will show how you can glue together the following terms together

  • select box
  • JSON
  • jQuery
  • *Javascript

* You might ask isn’t jQuery javascript? Yes indeed you are right, but jQuery is a framework making javascript programming a lot easier.

When I say plain javascript, or as some are calling it old school javascript, I meant to use the same task without using jQuery.

What select box is and types of select box options

Select box is one of the most used HTML element along with text input, radio option and the like.

Select box will allow you to provide dropdown options where you can select one or more items from it. You can make the select box to allow multiple selection using multiple attribute


Continue reading jQuery Select Option from Json

Passing composite object parameter to jersey in Restful Java web

How to pass complex object parameter to jersey

In this tutorial, I will try to show how to pass composite object as a parameter to jersey’s endpoint java application.

It seems like you are working on RESTful API based java application. In that case, chances are high that you are working with jersey.

If you are not familiar with jersey, jersey is an implementation of JAX-RS APIs. Hence, it is a seamless framework by which API based java projects can be implemented.

As of this writing, the current version of jersey is 2.23.
Continue reading Passing composite object parameter to jersey in Restful Java web

error = MongoId not found in symfony application

I got the MongoId not found error on the the application I recently moved to remote server.
The app is working fine locally and the problem appears to happen on the remote one only.
The problem seems obvious, and I checked the mongo version I have locally vs the version I have on the remote – they are different

I have newer version of mongo installed on the remote server. And the doctrine orm handling my mongo objects was also older.
I updated my doctrie orm on composer as

 "doctrine/orm": "2.4.6",

This took care of the problem – at least for now 😉

Using unquoted json formatted result set in symfony2 twig

In the request->process->response world of the MVC infrastructure, Symfony2 fits just perfect.

I was working on some data intensive site. All I want was to get the set of records from the mysql database and pass it to the front end through the controller. The front end wants the JSON format of the result set. The front end is getting the polished output of the twig

The problem
Front-end is not happy since it is getting quote-escaped version of JSON

The solution

Very very very simple. Rather than passing the json_encode(all_cars), where all_cars being the array of results coming from database or whatever it is, just pass the array it self

{{ all_cars | json_encode | raw }}

Yup, twig will not try to escape the quotes in this case since it is told to present raw.

Additional View

As you can see in this menu, both JSON and html views are together as combo. From design point of view this is not a good approach.

Better approach

You can bake the whole view right in the view and you might not want additional javascript logic on your view

OR
Have specific API to return 100% JSON response for the request like

some domain dot com/all/cars

By hitting this from, say your ajax call, you will be provided with json formatted list of all cars.

Since you are calling it from javascript, it will be directly coming to its home and no funny business would be there.
Then you will have another url call to load the page say:

some domain dot com/cars

where it will simply load the bare html format to the front-end and the front-end will know what to do with it.

Of course, this would have two trips and even more so it would be suitable for 80-90% of ajaxy sites..

EnJoY!

Browser automatically sorting json object based on key problem..

Json data got sorted on browser by its key

This a problem where the browser decides to sort the json you provided its own way rather than the one you sorted.

And it happens especially if you have key value pair in json payload you are passing.

So the scenario would be, there JSON data you are sending from the backend is sorted and you displayed it on front end with some javascript, but the result you are showing is not sorted with the original sorting order you set.

Solution

I observed that some browsers, at least Chrome, is doing the sorting in the situation that is discussed above.

For the fix: make the key of the json values you are passing string format rather than number

like

in place of using:
{1:"one", 4:"four"}

use

{"1":"one", "4":"four"}