请求.args.get对于ajax请求,返回none

2024-03-28 12:01:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在html5中实现就地编辑。试图实现这一点

http://w3lessons.info/2014/04/13/html5-inline-edit-with-php-mysql-jquery-ajax/

但是在flask中,使用ajax和jquery。 当我向flask app发送ajax请求时,它似乎无法访问我传递给“dd”标记的数据。这是我的路由功能

@app.route('/updateNode', methods=['POST'])
def updateNode():
 fieldToEdit = request.args.get('fieldToEdit', None)
 value = request.args.get('value', None)

 app.logger.debug(value)
 app.logger.debug(fieldToEdit)
 if value == None:
    return jsonify(success=0)
 else:
    return jsonify(success=1)

当我检查使用时总是看不到app.logger.debug(值)

视图中的调试:无

视图中的调试:无

我的Ajax代码是这样的

^{pr2}$

在进行ajax调用之前,我可以在开发人员工具控制台中看到正确的值。在

我的Html代码

<li>
  <dl>
   <dt>Age</dt>
   <dd id="editAge"contenteditable="true">40</dd>
  </dl>
</li>

runnable中的示例代码 http://code.runnable.com/me/VqbxNjRjLVNkRnxN

.html()应该只返回值,所以我不明白为什么请求.args.get无法从数据中提供的键值对检索值


Tags: 代码debugnoneapphttpflaskgetvalue
2条回答

出于这些目的,我不确定是否要发送实际的HTML值,因为它将有HTML标记。在

文件。在

html(): http://api.jquery.com/html/

In an HTML document, .html() can be used to get the contents of any element. If the selector expression matches more than one element, only the first match will have its HTML content returned. Consider this code:

$( "div.demo-container" ).html();

In order for the following 's content to be retrieved, it would have to be the first one with class="demo-container" in the document:

^{pr2}$

The result would look like this:

<div class="demo-box">Demonstration Box</div>

但是,正如您所看到的,HTML标记仍然存在。在

您应该使用text()

文件 http://api.jquery.com/text/

Unlike the .html() method, .text() can be used in both XML and HTML documents. The result of the .text() method is a string containing the combined text of all matched elements. (Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.) Consider the following HTML:

<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
  <ul>
    <li>list item 1</li>
    <li>list <strong>item</strong> 2</li>
  </ul>
</div>

代码$( "div.demo-container" ).text()将产生以下结果:

Demonstration Box list item 1 list item 2

我可以用这个代码来解决它

 <script type=text/javascript>
 $(function() {
  $('dd[contenteditable=true]').bind('blur', function() {
    $.getJSON('/updateNode', {
      a: $(this).attr("id"),
      b: $(this).text()
    }, function(data) {
    console.log("Ajax call was successful" + data);
   });
   return false;
  });
 });
</script>

但是,我不知道为什么它不能与$.ajax方法一起工作

相关问题 更多 >