Search code examples
phpjqueryajaxmockjax

TypeError: settings.data is undefined in MOCKJAX


I have mockjax code and it give me error.Here is my code

  $.mockjax({
    url: '*',
    responseTime: 2000,
    response: function (settings) {
        var query = settings.data.query,
            queryLowerCase = query.toLowerCase(),
            re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi'),
            suggestions = $.grep(countriesArray, function (country) {
                 // return country.value.toLowerCase().indexOf(queryLowerCase) === 0;
                return re.test(country.value);
            }),
            response = {
                query: query,
                suggestions: suggestions
            };

        this.responseText = JSON.stringify(response);
    }
});

Here is my ajax code

 $.ajax({
             async: false,
             url: 'coments.php?id='+$('#id').val()+'&cmnt='+$('#cmnt').val()+'&type=2',
             success: function(data) {
             alert('saved');
             }
});

It give me error TypeError: settings.data is undefined when i check console


Solution

  • This is a documented bug in Mockjax. Basically, the request matcher only works with data objects, not query strings. You could alter your $.ajax() call like so to match your mock:

    $.ajax({
        async: false,
        url: 'coments.php',
        data: {
            id: $('#id').val(),
            cmnt: $('#cmnt').val(),
            type: 2
        }
        success: function(data) {
            alert('saved');
        }
    });