需要关于ajax成功功能的建议

时间:2014-08-28 10:52:40

标签: javascript jquery ajax

我有小问题 - 我正在进行ajax查询以将数据传输到另一个php文件,一旦查询成功,我需要加载一个脚本。

$.ajax({
    type: 'POST',
    url: 'promote.php',    
    data: { country:country },    
    success: function() {
        //What to do here?
    }
});

脚本是:

<script type="text/javascript">
    var var1 = "d1dtGWhrOaxxUVCy%2FNjJwkpd9lCj%2FGijAGXS5d7LYPU%3D";
    var var2 = "jn9drdklwlr5hjYc2eHTCaXRjdpHDqhaqL5LXpsyc54%3D";
    var var3 = "E770DuKV8GKaHhaZO%2B%2F9sPzUI9mAUFVRrxRTH75pHvA%3D";
    var var4 = "LhWNAMH8CgveH%2FqRHKxqg8ebEVZKTX4pQw3pp5wTRl8%3D";
    var network = "xxxxxxx";
</script>
<script type="text/javascript" src="http://xxxxxxx/js/xx.js"></script>

所以任何建议如何在ajax查询成功时加载此脚本?我需要在此文件中执行此操作,请不要建议将脚本放在其他文件中并从中加载。那可能吗?

2 个答案:

答案 0 :(得分:0)

您可以使用它来附加标记:

var s = document.createElement("script");
s.type = "text/javascript";
s.src = "http://somedomain.com/somescript";
$("head").append(s);

答案 1 :(得分:0)

$.ajax({
type: 'POST',
url: 'promote.php',    
data: { country:country },    
success: function() {

 var script='<script type="text/javascript">';
     script+='var var1 = "d1dtGWhrOaxxUVCy%2FNjJwkpd9lCj%2FGijAGXS5d7LYPU%3D";';
     script+='var var2 = "jn9drdklwlr5hjYc2eHTCaXRjdpHDqhaqL5LXpsyc54%3D"';
     script+='var var3 = "E770DuKV8GKaHhaZO%2B%2F9sPzUI9mAUFVRrxRTH75pHvA%3D"';
     script+='var var4 = "LhWNAMH8CgveH%2FqRHKxqg8ebEVZKTX4pQw3pp5wTRl8%3D"';
     script+='var network = "xxxxxxx"';
     script+='</script>';
     script+='<script type="text/javascript" src="http://xxxxxxx/js/xx.js"></script>';
     $("head").append(script);
}
});