[TG2.1] Ajax Request

Django, Flask, Bottle, WSGI, CGI…
Antworten
nvidia
User
Beiträge: 31
Registriert: Freitag 11. Februar 2011, 16:46

Hallo ich möchte einen Ajax-Request mit jQuery machen. Aber ich komm mit dem Controller bei TurboGears nicht klar.
// content.html
<script>
$.ajax({
type: "POST",
url: "/content",
dataType: "JSON",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
</script>
Wie muss der Controller dazu jetzt aussehen? Der hier funktioniert nicht:

Code: Alles auswählen

    @expose('apmo.templates.content')
    def index(self):
        return dict()

     @expose('json')
     def content(self):
	pages = ['A','B']
	return dict(pages = pages)
deets

Was heisst "funktioniert nicht"? Was passiert, wenn du den Controller direkt ueber den Browser aufrufst? Gibt es einen Fehler? Was sagt Firebug oAE?
nvidia
User
Beiträge: 31
Registriert: Freitag 11. Februar 2011, 16:46

also wenn ich aufrufe
localhost:8080/index kommt die content.html aber es kommt zu keinem javascript alert.
Opera kriegt als Antwort 404 not found auf den ajax request
wenn ich den controller direkt aufrufe localhost:8000/content bzw. localhost:8080/content.json kommt 404 Not Found
=> Irgentwas stimmt mit dem controller nicht
deets

warum einmal localhost:8000, statt 8080?

Und fuer mich funktioniert es problemlos so:

Code: Alles auswählen



class RootController(BaseController):
    """
    The root controller for the jsontest application.

    All the other controllers and WSGI applications should be mounted on this
    controller. For example::

        panel = ControlPanelController()
        another_app = AnotherWSGIApplication()

    Keep in mind that WSGI applications shouldn't be mounted directly: They
    must be wrapped around with :class:`tg.controllers.WSGIAppController`.

    """

    error = ErrorController()

    @expose('jsontest.templates.index')
    def index(self):
        """Handle the front-page."""
        return dict(page='index')

    @expose("json")
    def test(self):
        return dict(foo="bar")

Aus einem frischen Quickstart
nvidia
User
Beiträge: 31
Registriert: Freitag 11. Februar 2011, 16:46

Also mit einem neuen Projekt funktioniert es.
Das war nur ein Schreibfehler war zweimal 8080.
Ich weiß nicht, warum das bei dem alten Projekt nicht funktioniert. Das ist absolut der identische Code.
Aber danke jetzt funktionierts. ;-)
Antworten