简体   繁体   English

请求的 URL 不允许使用该方法

[英]Method is not allowed for requested URL

I don't what is causing this to make error.Maybe I think it's a small thing to be changed but I am not able to find this.Please help me through this.我不知道是什么导致了这个错误。也许我认为这是一件需要改变的小事,但我找不到这个。请帮助我完成这个。 What i want to dis send the file address to server and format it for something,that code I haven't added but the address is not being formatted.Help will be appreciated我想将文件地址发送到服务器并对其进行格式化,该代码我没有添加,但地址没有被格式化。帮助将不胜感激

    <div class="container">
         <form class="form-signin" method="post" role="form">
             <h2 class="form-signin-heading">Please Sign Up </h2>
             <div>
             <input type="text" name="address" placeholder="Drivers" class="form-control col-3 mr-4">
        </div>
        <div>
            <button class="btn btn-outline-light d-inline-block" style="width: 150px;float: left;" id="upload">Upload</button>
        </div>
        </form>
     </div>
$(function() {
     console.log("working")
    $('#upload').click(function() {
        console.log("inside is working")
        $.ajax({
            url: '/signUpUser',
            data: $('form').serialize(),
            type: 'POST',
            success: function(response) {
                console.log(response)
                console.log(data.address);
            },
            error: function(error) {
                console.log(error);
            }
        });
    });
});
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/signUpUser', methods=['GET','POST'])
def signUpUser():
    if request.method=='POST':
        address=request.form['address']
    return jsonify({'address':address}); 

Method not allowed means the route has not been set up for GET or most likely POST , this error is coming from your API meaning you haven't specified on your route what method it should be so it fails.不允许的方法意味着尚未为GET或最有可能的POST设置路由,此错误来自您的 API 意味着您尚未在路由上指定它应该使用什么方法,因此它失败了。 I'm pretty sure it defaults to GET but if you're calling POST then it will complain at you with this error.我很确定它默认为GET但如果你调用POST那么它会向你抱怨这个错误。 Without seeing your api code that's about as much help as we can give.在没有看到您的 api 代码的情况下,我们可以提供尽可能多的帮助。

The problem is because you've not prevented the standard form submission.问题是因为您没有阻止标准表单提交。 This means that you're sending a standard form request to the current page, which is not configured to receive POST requests, hence the error you see.这意味着您正在向当前页面发送标准表单请求,该页面未配置为接收 POST 请求,因此您会看到错误。

To fix the problem attach a submit event handler to the form instead and call preventDefault() on the event which is provided as an argument to the handler function;要解决此问题,请将submit事件处理程序附加到form ,并对作为参数提供给处理程序 function 的事件调用preventDefault()

$(function() {
  $('form.form-signin').on('submit', function(e) {
    e.preventDefault();

    $.ajax({
      url: '/signUpUser',
      data: $(this).serialize(),
      type: 'POST',
      success: function(response) {
        console.log(response)
        // console.log(data.address); < commented out as 'data' is not defined anywhere
      },
      error: function(x, s, e) {
        console.log(x, s, e);
      }
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM