0

I have problem if i start project ajax first post get exception error undefined, BUT other posts work well. And if i set async:false i dont get errror but window.location doesnt work.

  $.ajax({
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                type: "POST",
                headers: headers,
                data: json,
                url: "/Controller/Action",
                cache: false,
                success: (result) => {
                    if (result.zprava === "good") {
                        window.location.assign(result.url);
                        return false;
                    }
                    else if (result.zprava === "low") {
                        alert("Lowly!");
                        return false;
                    }
                    else if (result.zprava === "bad") {
                        alert("bad ");
                        return false;
                    }
                    return false;
                },
                error: (exception) => {
                    alert("Mistake!" + " || " + exception + console.log(json));
                }
            });

I have tried everything but no one help me...

I try wrapping

ajax().done(function (result) {
                window.location.assign(result.url);
            }).fail(function () {
                alert("chyba");
            });

            function ajax() {
                return $.ajax({

ERROR IS ONLY AT FIRST TIME and then all going good...My Controller sending json but this POST not waiting ... This ajax doesnt want know about any response he instantly fired ERROR but method call back sucefull....and tell me again about sync ... If i tried set async: false its good but i need after succes windows.location.append() and this isnt work... :/ sorry for my english i am trying do my best! :D and thanks much I'll be glad for any response not like as ajax... :D :D

necik11
  • 41
  • 4
  • You can't return from a callback like that. You need to learn how to handle async code. – Carcigenicate Mar 10 '17 at 00:06
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Carcigenicate Mar 10 '17 at 00:07

1 Answers1

0

Ok i create ajax this ajax save to function ajax() and after call ajax.then().. but still not going then last chance set in ajax post: async:false and work well

necik11
  • 41
  • 4