AJAX returns HTML code with output

前端 未结 3 947
天涯浪人
天涯浪人 2021-01-18 23:42

After trying some solutions from this and many other questions I wasn\'t able to get what is exact problem in my code. My code

$(document).ready(function() {         


        
相关标签:
3条回答
  • 2021-01-19 00:18

    I tried a lot and finally i got the solution using below code.

    $.ajax({
         type: "POST",
         url: "page name/method name",
         data: '{ param 1: "value", param 2: "value" }',
         contentType: "application/json; charset=utf-8",
         success: function (data) {}
    });
    
    0 讨论(0)
  • 2021-01-19 00:32
    $.ajax({
        URL: "<?= base_url()?>/controller name/function name",  
        type: 'POST',
        data: {school_code:school_code,class_name:class_name},
        contentType: "application/json; charset=utf-8",
        success: function(res)
        {
            console.log(res);
        }
    });
    

    Actually the real problem in this case is URL. if the URL is not set properly then function will not call and Ajax will return the same page content in response. so please check your URL and set it like above.

    0 讨论(0)
  • 2021-01-19 00:38

    use strip_tags while sending data from server file if that is in php like below-

    <script src="jquery.js" type="text/javascript"></script>
    <script>
    $(document).ready(function() {
            $.ajax({
                type: "POST",
                url: "test2.php",
                contentType: "application/json; charset=utf-8",
                success: function(result) {
                    //alert(result);
                    $("#commmonname").html(result);
                }
            });
        });
    </script>
    
    <div id="commmonname"></div>
    

    SERVER file

    <?php
    $msg="<h2>HI</h2>";
    echo strip_tags($msg);
    ?>
    
    0 讨论(0)
提交回复
热议问题