weixin_33726943 2015-06-07 07:31 采纳率: 0%
浏览 22

Ajax发布返回未定义

I am trying to send the value of what is in the div's on the post via ajax.

Specifically I'm trying to send what is in the div slick-active, so in this example console.log(data.results); would show 4 and 6

Here is the html:

          <div class="slider single-item">
              <div class="slick-slide"><h3>1</h3></div>
              <div class="slick-slide"><h3>2</h3></div>
              <div class="slick-slide"><h3>3</h3></div>
              <div class="slick-slide slick-active"><h3>4</h3></div>
              <div class="slick-slide"><h3>5</h3></div>

          <div class="slider single-item">
              <div class="slick-slide slick-active"><h3>6</h3></div>
              <div class="slick-slide"><h3>7</h3></div>
              <div class="slick-slide"><h3>8</h3></div>
              <div class="slick-slide"><h3>9</h3></div>
              <div class="slick-slide"><h3>10</h3></div>

Here is the ajax/jquery

      $(document).ready(function(){
            $('#submit').on('click', function(e){
                  e.preventDefault(); // preventing default click action

                  $.ajax({
                    url: '/testing',
                    type: 'post',
                    data: JSON.stringify({results: $('#div.slick-active')}),
                      success: function (data) {
                        console.log(data);
                      // ajax success callback
                    }, error: function (response) {
                      alert('ajax failed');
                      // ajax error callback
                    },
                  });
                });

Serverside Flask route

@app.route('/testing', methods=['GET', 'POST'])
def testing():
        r = request.json
        print r
        return "data recived is %s" % r

Which returns console.log r as None, which should instead return 4,6

  • 写回答

2条回答 默认 最新

  • weixin_33721427 2015-06-07 07:38
    关注

    Succes callback returns the response from server, not the parameters that you provide on the request. Try just console.log(data) and see what the result is. It can be empty. Additionally, check the status of the returned response, if you save posted data on the server side, it should return 201 Created.

    Just a hint: from jQuery 1.8 the .success(..) and .error(..) methods are deprecated. From jQuery documentation:

    Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

    评论

报告相同问题?