Load Json Search in JSP

Asked

Viewed 643 times

0

Good Morning I’m trying to make this example in my project. It’s a simple change of Select when you choose State Change Cities in another Select.

https://gist.github.com/ografael/2037135

But I can’t find the variable, estados_cidades.json .

I tried to bring as script
<script id="estados_cidades.json" src="GerenciaTI/Scripts/estados_cidades.json" ></script>

But he doesn’t bring the data through Jquery

I’m doing on JSP pages, what mistake I made ?

Or if there’s a better way to do it.

1 answer

1


Follow the example working...

		$(document).ready(function () {
		
			$.getJSON('https://gist.githubusercontent.com/ografael/2037135/raw/5d31e7baaddd0d599b64c3ec04827fc244333447/estados_cidades.json', function (data) {
				var items = [];
				var options = '<option value="">escolha um estado</option>';	
				$.each(data, function (key, val) {
					options += '<option value="' + val.nome + '">' + val.nome + '</option>';
				});					
				$("#estados").html(options);				
				
				$("#estados").change(function () {				
				
					var options_cidades = '';
					var str = "";					
					
					$("#estados option:selected").each(function () {
						str += $(this).text();
					});
					
					$.each(data, function (key, val) {
						if(val.nome == str) {							
							$.each(val.cidades, function (key_city, val_city) {
								options_cidades += '<option value="' + val_city + '">' + val_city + '</option>';
							});							
						}
					});
					$("#cidades").html(options_cidades);
					
				}).change();		
			
			});
		
		});
		
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<body>

<form>
		
		<!-- Estado -->
		<select id="estados">
			<option value=""></option>
		</select>
		<select id="cidades">
		</select>
	
</form>


</body>
</html>

  • Yes it worked, but then you’re pulling their link Online.

  • But to pull Local ????

  • just save the JSON locally and pull it, can’t do so? I’ll put together an example

  • this, please

  • Here it worked, Voce can place the entire link of the saved file locally. Ex: your file is saved inside the json folder. would look like this. $. getJSON('http://localhost/MEUPROJETO/json/estados_cidades.json'

  • Great, it worked thanks. Just one last question about this code. By which intendi he assigns the value of the right states, I’m pulling request.getParameter("states") and it usually comes now when I request.getParameter("cities") it brings null in Servlet ? the onchagen event only changes the value of states

  • If it worked could mark my answer? in this case, You need to do logic again

  • i am very slow even ta all right thanks Rafael, I put the wrong name on the variable kkkk

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.