收到数据后,Ajax'POST'再次被修改

时间:2017-10-31 01:11:54

标签: javascript jquery ajax object

我在Ajax发布后从后端获取数据,该数据将包含值列表。当用户从列表中选择一个值时,我希望它更新到我的对象并再次发布ajax调用。

我在这里遇到的问题是我无法再次选择值,我只需要一次ajax调用即可。嵌套的ajax调用不够

var searchObject = {
  "research": "test"
}
$.ajax({
  method: method,
  data: JSON.stringify(searchObject), // while making next call the searchObject should have. "list":value of x in success
  contentType: "application/json",
  url: requestUrl
})
 .success(function(data) {
  var x = data.list2


}).error(function() {

});

2 个答案:

答案 0 :(得分:0)

我认为你可以使用syn ajax请求而不是asyn请求。你可以从jquery api Jquery ajax

看到它

并且您可以尝试这样的代码:

var searchObject = {
  "research": "test"
}
let x = undefined;
$.ajax({
  async:false,
  method: method,
  data: JSON.stringify(searchObject), // while making next call the searchObject should have. "list":value of x in success
  contentType: "application/json",
  url: requestUrl
})
 .success(function(data) {
   x = data.list2


}).error(function() {

});

//Another syn ajax post 
$.ajax({
  async:false,
  method: method,
  data: JSON.stringify(x), // while making next call the searchObject should have. "list":value of x in success
  contentType: "application/json",
  url: requestUrl
})
 .success(function(data) {
   console.log(data)

}).error(function() {

});

答案 1 :(得分:0)

如果你正在使用带Ajax的POST,这应该为你做。只需创建一个文件并将其命名为testing.php并在输入字段中输入一个值,您将看到ajax成功向backEnd发送数据。希望它有所帮助!

<?php

 $data = array();
  if(isset($_POST['research'])){
  $data = $_POST['research'];       
  echo json_encode($data);  
 die();      
 }
 ?>
 <html>
 <head>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
 </head>

 <body>

  <form>
    Enter value: <input type="text" id="someID"><br>
<input value="submit" type="submit">
 </form>
   <p id="msg"><p/>

<script type = "text/javascript">

$("form").on("submit", function(e){

    e.preventDefault();
    var emailfield = $("#someID").val();
    var myText ='You entered: ' + emailfield;

    $.ajax({
        url: "testing.php",
        method: "POST",
        dataType: "json",
        data: {research: myText},
        success: function (result) {
            alert("result: " + result);
            $("#msg").html(result);
        },
        error: function (params) {
            alert("error");
        }
    });
    });

 </script>
</body>
</html>