从Flask发送POST请求到PHP脚本

我正在尝试向Flask的PHP脚本发送POST请求。 我正在使用Datatables Editor从后端数据库获取信息。 数据表将发送POST请求以获取DOM准备好时所需的数据。 但是,我收到以下错误:</ p>


方法不允许所请求的URL不允许使用该方法。</ p>
</ blockquote> \ n

这是路线:</ p>

</ p>





   @ bp.route('/ my_php',methods = ['GET','POST']) 
def php_post():
url ='http:// localhost:5000 / static / datatables-plugins /Editor-PHP-1.7.4/examples/php/staff.php'
headers = {

'内容类型':'application / x-www-form-urlencoded; charset = UTF-8',
'接受':'application / json,text / javascript,* / *; q = 0.01',
'接受编码':'gzip,deflate,br',
'X-Requested-With':'XMLHttpRequest'

}

data = urllib.parse.urlencode(request.form).encode('utf-8')
esp = requests.post(url,data = data,headers = headers)
return json.dumps(resp.content).encode('utf-8')</ code> </ pre>

</ div>

</ div>

我认为这可能与指定允许的方法有关,但是我不知道如何在</ em> Flask路径中调用另一个URL的请求时显式允许GET / POST方法。 </ p>
</ div>

展开原文

原文

I'm trying to send a POST request to a PHP script from Flask. I am using a Datatables Editor to get information from a backend database. Datatables will send a POST request to get the data it needs when DOM is ready. However, I'm getting the following error:

Method Not Allowed The method is not allowed for the requested URL.

Here's the route:

@bp.route('/my_php', methods=['GET', 'POST'])
def php_post():
    url = 'http://localhost:5000/static/datatables-plugins/Editor-PHP-1.7.4/examples/php/staff.php'
    headers = {

        'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate, br',
        'X-Requested-With': 'XMLHttpRequest'


    }
    data = urllib.parse.urlencode(request.form).encode('utf-8')
    resp = requests.post(url, data=data, headers=headers)
    return json.dumps(resp.content).encode('utf-8')

I think this may have to do with specifying allowed methods, however I don't know how to explicitly allow for GET/POST methods when a request for another URL is called within the Flask route.

</div>

duanduoding2238
duanduoding2238 PHP脚本允许POST,因为它在我使用sudo/usr/bin/php-Slocalhost:80运行测试http服务器时有效。当我在Firefox中使用开发人员工具时,我可以清楚地看到POST请求并从PHP文件返回。
大约 2 年之前 回复
dongliang7545
dongliang7545 运行的php脚本可能不允许其他方法。kite.com/python/docs/requests.post
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问