You are not logged in.

1

Wednesday, September 8th 2010, 8:48pm

Problem mit JSON

Hi,
bin ganz neu im Gebiet JSON und habe ein Problem:
Ich schicke eine Anfrage per jQuery-Ajax an eine PHP-Datei, die mir die Zeit und eine ID zurückgibt.
Das alles wird in php per json_encode passend gemacht.
Das kommt also an:

Source code

1
{"n_time": 1283971437, "id": 1}

Was ist an dem Javascript falsch:

JavaScript Code

1
2
3
4
5
6
7
8
9
$.ajax({
		url: m_url,
		type: 'post',
		dataType: 'json'
	},
	function (data) {
			alert(data.n_time);
		}
		);

2

Wednesday, September 8th 2010, 9:39pm

Also wenn data wirklich wie oben aussieht, dann würds funktionieren.
JSON kannst du das ganz gut in der Firebug Konsole testen

Source code

1
2
3
4
>>> x = {"n_time": 1283971437, "id": 1};
Object n_time=1283971437 id=1
>>> x.n_time;
1283971437


Aber du übergibst deine Callback Methode falsch!
Entweder du übergibst sie dem Attribut success

JavaScript Code

1
2
3
4
5
6
$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});


oder nimmst einfach den Shortcut: http://api.jquery.com/jQuery.getJSON/

3

Friday, September 10th 2010, 10:09am

Danke!
Das war das Problem, war das noch so von Prototype gewöhnt...

Similar threads

Social bookmarks