R
R
rosrost2018-07-23 20:15:50
JavaScript
rosrost, 2018-07-23 20:15:50

Ajax request not sent?

Good evening, to the point:
request:

$(document).ready(function () {
        $('.on-tree').click(function () {
            var mySponsorNumber = $(this).attr('id'),
                url = '/all/add_item_tree.php';
            $.ajax({
                type: "POST",
                url: url,
                data: {treeElm: mySponsorNumber},
                dataType: 'json',
                success: function (result) {
                    if (result) {
                        alert('ok');
                        console.log(result);
                    } else {
                        alert('error');
                    }
                },
                error: function () {
                    alert('error2');
                }
            });

        });
    });


file add_item_tree.php

<?php
session_start();
require_once("mysql/db.php");
$itemTree = $_POST['treeElm'];
if($itemTree){
    $out = array(
        'testAjax' => $itemTree
    );
}else{
    $out = array(
        'testAjax' => 'syka blyat'
    );
}
header('Content-Type: text/json; charset=utf-8');

echo json_encode($out);


While looking for errors, I cut the code a lot, when clicked, an error occurs: function () { alert('error2'); }(window)
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaximMRX, 2018-07-23
@rosrost

Obviously a bug on the server. Most likely, not valid JSON is coming, get the response that comes from the server, and check it for validity with any online service

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question