【问题标题】:REST request to server with jQuery使用 jQuery 向服务器发送 REST 请求
【发布时间】:2015-01-06 11:34:17
【问题描述】:

问题: 出现错误 404。我认为问题在于形成 URL。拜托,谁能指出我的错误。我已经尝试了一段时间的不同变体,但无法使其正常工作。

我的代码摘录:

服务器

class Nearest_banks(webapp2.RequestHandler):

    def main_func(self):
        body_values = {
           'radius': default_radius,
         }

        template = jinja_env.get_template('nearest_banks.html')
        self.response.out.write(template.render(body_values))


    def get(self):
        # some code here

    def post(self, entered_radius):
        return webapp2.Response('Done')

application = webapp2.WSGIApplication([
   webapp2.Route(r'/nearest_banks', handler=Nearest_banks, name='n_banks', handler_method='main_func'),
   webapp2.Route(r'/nearest_banks/default_radius', handler=Nearest_banks, name='default_radius', handler_method='get'),
   webapp2.Route(r'/nearest_banks/radius/(\d+)', handler=Nearest_banks, name='new_radius', handler_method='post'),
], config=session_module.config, debug=True)

客户

$( "#change_radius" ).click(function(){
    var new_radius = $("#radius").val();

    $.ajax({
        type: "post",
        url: '/nearest_banks/radius/' + new_radius,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function() {
            $("#places").html("OKKK");
        },
        error: function(){
            alert("error");
        }
    });

【问题讨论】:

    标签: jquery python ajax google-app-engine


    【解决方案1】:

    试试这个

    url_data= '/nearest_banks/radius/' + new_radius;
    $.ajax({
            type: "POST",
            url: ''+url_data+'',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            xhrFields: {withCredentials:true},
            success: function() {
                $("#places").html("OKKK");
            },
            error: function(){
                alert("error");
            }
        });
    

    希望对你有帮助

    【讨论】:

    • 谢谢.. 但也不能用它,我仍然认为问题出在服务器端
    • @Elena 检查请求是否到达您的服务器
    • 当我在 webapp2.WSGIApplication([])中将 root 更改为 '/nearest_banks/radius/' 时,请求正在命中并且应用运行良好> 因此在 ajax 请求的 url 中。所以当我不将 new_radius 变量连接到 url 时它可以工作
    • @Elena 我已经更新了答案尝试如果它现在有效。
    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 2018-08-28
    • 2019-03-16
    相关资源
    最近更新 更多