0

I have problem with jQuery Ajax calling method success. I tried everything but success is not calling. My code looks like

$('#target').submit(function(event) {
    // get the form data
    var formData = {
        'name': $('input[name=name]').val(),
        'surname': $('input[surname=surname]').val(),
    };

    // process the form
    $.nette.ajax({
        type: 'POST', 
        url: {link Admin:uploadData}, 
        data: formData, // our data object
        dataType: 'json',
        async: false,
        contentType: "application/json",
        off: ['unique'],
        success: function(payload){
            console.log(payload.message.lol);
        }
    })
    event.preventDefault();
});

I am using nette web framework. When I debug code in Firefox I get a response like this:

enter image description here

Tkanks for the advice.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

1 Answers1

1

You don’t even need nette.ajax for this, plain jQuery.ajax will suffice.

$('#target').submit(function(event) {
    // get the form data
    var formData = {
        'name': $('input[name=name]').val(),
        'surname': $('input[name=surname]').val(),
    };

    // process the form
    $.ajax({
        type: 'POST',
        url: $(this).attr('action'), // assuming #target is a form, we can use its action
        data: formData, // our data object
        dataType: 'json',
        contentType: 'application/json',
        success: function(payload) {
            console.log(payload.message.lol);
        }
    });

    event.preventDefault();
});

Also, as @Rory McCrossan commented, async property will only make your site freeze, don’t use it unless you really know you need it.

Jan Tojnar
  • 5,306
  • 3
  • 29
  • 49
  • Thanks with this url, but when I use jQuery.ajax, I get as answer whole HTML page without JSON data. – Peťo Hnat Jul 06 '16 at 14:01
  • jQuery should add `X-Requested-With: XMLHttpRequest` header, which you can detect using [`isAjax`](https://api.nette.org/2.4/Nette.Application.UI.Presenter.html#_isAjax) method in the presenter and send a JSON payload in that case. – Jan Tojnar Jul 06 '16 at 14:46
  • yeah I know but its not working. I got as response always whole html page. – Peťo Hnat Jul 06 '16 at 20:54