drol55885602 2017-07-06 13:30
浏览 128
已采纳

AJAX | 使用alert(); 其他文件中的函数不起作用

I'm working on Ajax and still learning to use it properly. I'm wondering why in one side of Ajax alert(); works but on the other side - no.

It's pretty basic code:

$('.naudoti_papildus_visiems').click(function() {
    var n;                  

    $.post('/ajax/naudoti_papildus_visiems.php', { status : 1, fermos_id : '<?php echo $id_kurat; ?>', gyvulio_tipas : '<?php echo $type_zagon; ?>' },
    function(data) {
        var typeclick = data.typeclick,
            typeu = data.typeu,
            stil = data.stil,
            stildell = data.stildell;

        $.noty.closeAll();
        if (n) {
            n.setText('<b>' + data.message + '</b>');
            n.setType(typeu);
        } else {
            n = noty({
                text: '<b>' + data.message + '</b>',

                type: typeu,
                dismissQueue: false,
                layout: 'top',
                theme: 'defaultTheme',
                timeout: 3000,
            });
        }
    }, 'json');
});

"naudoti_papildus_visiems.php" content:

<script>alert(9);</script>

I've tested one thing: using alert(); function inside an Ajax, it works:

$('.naudoti_papildus_visiems').click(function() {
var n;                  

$.post('/ajax/naudoti_papildus_visiems.php', { status : 1, fermos_id : '<?php echo $id_kurat; ?>', gyvulio_tipas : '<?php echo $type_zagon; ?>' },
function(data) {
    var typeclick = data.typeclick,
        typeu = data.typeu,
        stil = data.stil,
        stildell = data.stildell;

    alert(9); // works

    $.noty.closeAll();
    if (n) {
        n.setText('<b>' + data.message + '</b>');
        n.setType(typeu);
    } else {
        n = noty({
            text: '<b>' + data.message + '</b>',

            type: typeu,
            dismissQueue: false,
            layout: 'top',
            theme: 'defaultTheme',
            timeout: 3000,
        });
    }
}, 'json');
});

Chrome console doesn't show any errors. Any help would be appreciated.

loelsonk: still doesn't work. Am I doing something wrong?

    $('.naudoti_papildus_visiems').click(function() {
    var n;                  

$.ajax({
  url: '/ajax/naudoti_papildus_visiems.php',
  type: 'POST',
  dataType : 'json',
  data: { status : 1, fermos_id : '<?php echo $id_kurat; ?>', gyvulio_tipas : '<?php echo $type_zagon; ?>' },
  success: function(data) {
        var typeclick = data.typeclick,
            typeu = data.typeu,
            stil = data.stil,
            stildell = data.stildell;

        $.noty.closeAll();
        if (n) {
            n.setText('<b>' + data.message + '</b>');
            n.setType(typeu);
        } else {
            n = noty({
                text: '<b>' + data.message + '</b>',

                type: typeu,
                dismissQueue: false,
                layout: 'top',
                theme: 'defaultTheme',
                timeout: 3000,
            });
        }

        $('#data').html(data);
    }
});
});
  • 写回答

1条回答 默认 最新

  • douzhenao6515 2017-07-06 13:41
    关注

    If you would like to execute js code you mentioned <script>alert(9);</script> from your request response append it to a #temp container.

    First create #temp container we append response:

    <div id="temp"></div>
    

    Instead of $.post we use $.ajax.

    $.ajax({
      url: '/ajax/naudoti_papildus_visiems.php',
      type: 'POST',
      data: { status : 1, fermos_id : '<?php echo $id_kurat; ?>', gyvulio_tipas : '<?php echo $type_zagon; ?>' },
      success: function(response) {
        // Do whatever you want here
    
        // response contains js code to be executed
        // Now your code gets executed
        $('#temp').html(response);
      }
    }) 
    

    EDIT:

    Here is small working example to show you how it works:

    We have two files:

    file 1: index.php

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title>title</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <style>
                body {
                    padding-left: 250px;
                }
                #temp {
                    border: 2px solid #000;
                    padding: 20px;
                    width: 300px;
                    min-height: 200px;
                }
            </style>
      </head>
        <body>
            <div>
                <h1>Ajax</h1>
                <button id="button" type="button">Perform ajax</button>
    
                <div id="temp"></div>
            </div>
    
            <script>
                $('#button').click(function(e) {
                    $.ajax({
                    url: '/ajax/naudoti_papildus_visiems.php',
                    type: 'POST',
                    data: { status : 1, fermos_id : 888, gyvulio_tipas : 'some_type' },
                    success: function(response) {
                        // Do whatever you want here
    
                        // response contains js code to be executed
                        // Now your code gets executed
                        $('#temp').html(response);
                    }
                    });
                });
            </script>
        </body>
    </html>
    

    file 2: /ajax/naudoti_papildus_visiems.php (file is inside ajax dir)

    <?php
    
    echo 'test';
    echo '<script>alert(9);</script>';
    

    See how it works:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)