简体   繁体   中英

Error in Ajax code to get response from codeigniter

I want to send my request to CodeIgniter to get response from my database:

$(document).ready(function(){

$('.confirm_send').on('click',function(){

var base = '<?php echo base_url(); ?>';

var confirm_send_id=$(this).attr('id');


        $.ajax({
            'url': base + 'my_site/confirm_send',
            'type':'post',
            'dataType':'json',
            'data':{
                'id':confirm_send_id
            },
            'success':function(){
                alert("sucssess");},

            'error':function(){
                alert("Error");
            }


        });//end of ajax confirm send button

    });//end of confirm send button

    });//end of jquery

But I am getting an error.

Try This:

 $('.confirm_send').on('click',function(){

     var base = '<?php echo base_url(); ?>';
     var confirm_send_id = $(this).attr('id');

     $.ajax({
        url: base + 'my_site/confirm_send',  //make sure it is a valid path
        type: 'POST',
        data:({'id':confirm_send_id}),
        success :  function(response){
             alert("Success");
        },

        error : function(response){
            alert("Error");
        }
    });//end of ajax confirm send button


});

sa server side coding issue that这就是为什么它会出现sa server side coding issue that所以您可以使用响应窗口检查错误,它会显示编码错误。

You can't inject PHP code into JavaScript. Remember that whatever script or css that you define will be run in the user's browser - and browsers don't interpret PHP code neither have direct access to your server.

You've gotta hard-code the base_url variable. If you don't know it just echo base_url(); on a page and copy it.

$(document).ready(function(){

$('.confirm_send').on('click',function(){

var base = 'http://yourproject_base_url'; //Set the full base url manually here. 

var confirm_send_id=$(this).attr('id');
    $.ajax({
        'url': base + 'my_site/confirm_send',
        'type':'post',
        'dataType':'json',
        'data':{
            'id':confirm_send_id
        },
        'success':function(){
            alert("sucssess");},

        'error':function(){
            alert("Error");
        }


    });//end of ajax confirm send button

});//end of confirm send button

});//end of jquery

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