Ajax Jquery – zpracovani POST pozadavku

Piticu
Člen | 93
+
0
-

Pouzil jste prosim nekdo ajax Jquery? nevim jak zkontrolovat/zpracovat data v Presenteru. Mam nasledujici kod:

$('#sendChanges').click(function(e) {
    var navValues = document.getElementById("sortable").getElementsByTagName("li");
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: {link menuOrder!},
        dataType: "text",
        async:false,
        data: $(navValues).serializeArray(),
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            alert("ok");
        },
        error: function (textStatus, errorThrown) {
            alert('no');
        }

    });
});

Vyskoci mi alert = ok. Pak mam presenter:

public function handleMenuOrder(){
    echo "Something";

}

Ktery ale nic nevypise. Jak mam ted zkontrolovat zda je vse v poradku nebo spis JE VSECHNO V PORADKU?

CZechBoY
Člen | 3608
+
0
-

Ja si predavam vzdy json z presenteru $this->sendJson(...)

v js pouzivej async requesty vzdy.
data z presenteru jsou dostupna v promenne data (success callback).

Piticu
Člen | 93
+
0
-

CZechBoY napsal(a):

Ja si predavam vzdy json z presenteru $this->sendJson(...)

v js pouzivej async requesty vzdy.
data z presenteru jsou dostupna v promenne data (success callback).

Muzes mi prosim napsat priklad co se tyce dat z presenteru (success callback)? Diky

CZechBoY
Člen | 3608
+
+1
-

Vezmu-li tvůj příklad

$('#sendChanges').click(function(e) {
    var $navValues = $("#sortable").find("li");

    e.preventDefault();

    $.ajax({
        type: "POST",
        url: {link menuOrder!},
        data: $navValues.serializeArray(),
        success: function (response) {
            if (response.hasOwnProperty('success')) {
				if (response.success) {
					alert("OK");
				} else {
					alert("Error");
				}
			} else {
				alert("Error: no success key");
			}
        },
        error: function (textStatus, errorThrown) {
            alert("Error: " + textStatus);
        }

    });
});
class MenuOrderPresenter extends UI\Presenter
{
	public function handleMenuOrder()
	{
		try {
			$this->model->orderMenu();
			$success = true;
		} catch (SomeException $exception) {
			$success = false;
		}

		$this->sendJson((object)[
			'success' => $success,
		]);
	}
}