1

When I click delete button confirmDelete function call and it post the parameters as json below. it comes back from server page and enter the success method but without any value.. not null just blank string:"" I cant figure out whats the problem I post and get datas inside the form in same page that works fine but this isn't.. what I am missing ?

confirmDelete = function()
{
    var data = {
        "action": "DeleteLocalObject",
        "objectName":intenttoDeleteobjectTitle
    };
    $.ajax({
        url:"../../Controller/ObjectController.php5",
        type:"POST",
        //contentType: "application/json",
        data: data,
        success: function(data) {
            debugger
            if(data.Passed)
            {               
                setTimeout(function () {
                    location.reload(true);
                }, 1500);
            }
            else
            {

            }
        },
        error:function(a, b, c){

        }
    });
}

and here response page:(I don't implement delete yet just want to check request is ok)

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
    if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
        $action = $_POST["action"];

        switch($action)
        { //Switch case for value of action
            case "CreateLocalObject":
                $controllerObject->createNewLocalObject($_POST["objectname_new"],$_POST["objectcomment_new"],$_POST["type_new"],$_POST["ipaddress_new"]);
                break;
            case "DeleteLocalsObject":
                $result =$_POST["objectName"];//this must get value
                echo json_encode($result);//and this must print data to success method
                break;
        }

I assumed it will return the same object name I posted.

1 Answer 1

1

You are not sending key - value pairs to the server but a string:

data: JSON.stringify(data),

So you cannot access it using the $_POST global; your variable will be empty so your result is empty.

You should send your data as an object of key-value pairs instead:

data: data,

And to have jQuery automatically parse the returned data, you should add a data type:

dataType: 'json',
data: data,
Sign up to request clarification or add additional context in comments.

2 Comments

@MehmetYenerYILMAZ You should add the current code under your original question and add some var_dump()'s / console.log()'s to see what your values are.
I found this never step into switch.. now it worked and returned exactly what I want. just u said I must not post json string.. thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.