简体   繁体   English

Javascript附加到从php回显的变量数据中

[英]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. 我将valdat变量发送到url,然后通过php文件对其进行处理,然后将其回显到应用程序中。 How can i append to a variable x the data the alert message displays? 如何将警报消息显示的数据附加到变量x?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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