简体   繁体   中英

Javascript append to variable data echoed back from php

I have this lines of code placed inside an app i am currently developing. I send to the url the valdat variable which is then processed through a php file and then echoed back to the app. How can i append to a variable x the data the alert message displays?

var valdat="foo";

$.ajax({
   url: 'http://www.link./file.php',
   type: 'POST',
   data: {
     valdat:valdat

   },

   error: function(data) {
   alert(data);

   },

   success: function(data) {
   alert(data);
},

});

If you mean append the returning value directly to a known variable you can just use += :

var valdat  ="foo";
var x       = "Something";

$.ajax({
   url: 'http://www.link./file.php',
   type: 'POST',
   data: {
     valdat:valdat

   },

   error: function(data) {
        x += data; 

        alert(x); //Will output "Something" + the content of `data`

   },

   success: function(data) {
        x += data; 

        alert(x); //Will output "Something" + the content of `data`
    },

});

解决了它... ajax调用正在异步执行,这意味着它与JS的其余部分并行运行,这意味着编译器将执行以下操作:1)valdat = foo 2)x = bar 3)h = x 4) ajax调用,所以程序不知道x是谁,因为它直到结尾都没有一个https://stackoverflow.com/a/5936026/5467736

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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