AJAX邮政储存JSON与Python和JavaScript邮政、AJAX、JSON、JavaScript

2023-09-10 14:35:28 作者:Étreindre moi 拥我吗

我一直有问题,让AJAX正确张贴JSON。该应用程序的目的是要在谷歌的App Engine托管。但是,我有什么不发布数据。

I have been having problems with getting AJAX to post JSON correctly. The application is intended to be hosted on Google App Engine. But what I have does not post data.

的Python

 mainPage = """
    <html>
    html is included in my python file.
    </html>
    """ 

    class JSONInterface(webapp2.RequestHandler):
    def post(self):
        name =self.request.get('name')
        nickname =self.request.get('nickname')

        callback = self.request.get('callback')
        if len(name) > 0 and len(nickname) >0:
            newmsg = Entry(name=name, nickname=nickname)
            newmsg.put()
        if len(name)>0:
            self.response.out.write(getJSONMessages(callback))
        else:
            self.response.out.write("something didnt work")

    def get(self):
        callback = self.request.get('callback')
        self.response.out.write(getJSONMessages(callback))

该处理器是为了处理来自Web应用程序的Ajax调用。我不确定我是否需要JavaScript来与我的主网页,以便相关的这样做,因为我还没有发现关于这些事与我的搜索。

This handler is meant to handle the Ajax calls from the web app. I am unsure if I need javascript to be associated with my main page in order to do so, as I haven't found information on it yet with my searches.

Javascript的

Javascript

$(document).ready( function() {

    $("#post").bind('click', function(event){
           var name = $("#name").val();
           var nickname = $("#nickname").val();

            postData = {name: name, nickname: nickname, callback: "newMessage"};

        $.ajax({
            type: "POST",
            url: "http://localhost:27080/json",
            data: postData,
            dataType: "json",
            done: function() {
                // Clear out the posted message...
                $("#nickname").val('');
            },
            fail: function(e) {
                confirm("Error", e.message);
            }
        });
        // prevent default posting of form (since we're making an Ajax call)...
        event.preventDefault();
    });

的JavaScript后

The Javascript for the post

有人可以告诉我,我怎么能解决我遇到的问题。感谢您的时间和帮助。

Can someone advise me on how I could resolve the problem I am having. Thanks for the time and help.

推荐答案

对于初学者来说,AJAX调用pretty的接近。完整路径

For starters, the ajax call is pretty close. The full path

"http:://localhost:27080/json"

是没有必要的,相对路径将工作,但是没问题的。

is not necessary, the relative path will work, but that is not the problem.

您的回调,因为它的立场,将作为成功:

Your callback, as it stands, will work as 'success':

success: function(response) {
            alert(response);

            // Clear out the posted message...
            $("#nickname").val('');
        }

不过,这种回调被淘汰,有利于other方法的。 完成应该捆绑,像这样:

However, this callback is being phased out in favor of other methods. 'Done' should be chained like so:

 $.ajax({
        type: "POST",
        url: "/json",
        data: postData,
        dataType: "json"            
    }).done(function(data){
        console.log(data);
    });

此外,可能存在在服务器上的问题。如果你使用一些日志记录,您将看到的数据确实被发送到服务器。

Also, there might be problems on the server. If you use some logging, you will see that the data is indeed being sent to the server.

import json ## we'll get to this below
import logging
class JSONInterface(webapp2.RequestHandler):
def post(self):
    name = self.request.get('name')
    logging.info(name) ## will print the value of 'name'

除非你的Python功能getJSONMessages(回调)返回一个JSON对象,你的回调将不会被调用,即使你加的响应参数。 在你的Python code:

Unless your python function getJSONMessages(callback) is returning a json object, your callback will not be called, even after you add the response parameter. In your python code:

import json
import logging
class JSONInterface(webapp2.RequestHandler):
    def post(self):
        callback = self.request.get('callback')
        logging.info(callback) # will print correctly
        self.response.out.write(json.dumps(callback)) 

使用 json.dumps方法 EN codeS的传递对象为JSON,这是你的Ajax对象所期待的。

Using the json.dumps method encodes the passing object to json, which is what your ajax object is looking for.