Uncaught SyntaxError: Unexpected token when using jQuery Ajax in Codeignitor

I am getting the error "Uncaught SyntaxError: Unexpected token" when using jQuery Ajax in Codeignitor.

Here is my code:

function add_to_shopping_cart(base_url){

$.ajax(function(){
    url: base_url+'cart/add_to_cart',
    type: 'post',           
    data: $('#product_form').serialize(),
    dataType: 'html',

    success: function(html){
    }
});     
}

The error is on the line "type: 'post',"

I have done ajax functions thousands of times and can't see what's causing this thanks


That code makes no sense. Remove the function() part in the $.ajax call:

function add_to_shopping_cart(base_url){
    $.ajax(/*no function() here*/{
        url: base_url+'cart/add_to_cart',
        type: 'post',           
        data: $('#product_form').serialize(),
        dataType: 'html',

        success: function(html){
        }
    });     
}

What you pass to ajax should be an object initializer, not a function. If it were a function, the content would need to be function code rather than a set of property initializers.

链接地址: http://www.djcxy.com/p/46018.html

上一篇: AngularJS未捕获的SyntaxError:意外的令牌:

下一篇: 未捕获SyntaxError:在Codeignitor中使用jQuery Ajax时出现意外的令牌