1

I have been at this for hours trying everything I could possibly find suggested on the web, however it continues to fail. When I encode function parameters with JSON.stringify and then pass it to my PHP handler via AJAX, I am getting an Alert pop-up saying parsererror.

It is apparently an issue within the Javascript but I cannot determine where at this point.

Can anyone perhaps spot what I am doing wrong here?

            // JSON Encode to pass via AJAX to PHP
            var params = { "id": player };
            var params2 = JSON.stringify(params);

            // {"id":"939952da-23df-4ff1-b66c-61018b621645"}
            console.log(params2);

            cellOptions.innerHTML = "<button onclick='sendAction(\"kickPlayer\"," + params2 + ")'>Kick</button>";

AJAX

function sendAction(cmd, params)
{
    $.ajax({
        type: "POST",
        url: "handler.php",
        data: { 'action': cmd, 'params': params },
        dataType: 'json',
        success: function(data)
        {
            if (data['status'] == 'failed')
            {

            }
            else if (data['status'] == 'success')
            {

            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        { 
            alert("Status: " + textStatus); alert("Error: " + errorThrown); alert("Message: " + XMLHttpRequest.responseText);
        }
    });
};
5
  • Put console.log(params) in the beginning of sendAction. What does it print? Commented Feb 4, 2015 at 14:33
  • Object {id: "939952da-23df-4ff1-b66c-61018b621645"} Commented Feb 4, 2015 at 14:34
  • @Rory I already tried this prior without using stringify. I was just passing the object directly and also did not work, where I encountered a discussion saying to use JSON instead Commented Feb 4, 2015 at 14:36
  • The problem is with the server response, request looks fine. What do you get from backend? Commented Feb 4, 2015 at 14:36
  • What is the exact text in the alert box ? That is very important in understanding the error. Commented Feb 4, 2015 at 14:44

2 Answers 2

3

By coding this :

dataType: 'json',

you tell jQuery to expect a JSON String in the response (from the backend, i.e. our PHP code). So on the callback, jQuery parses the string, and since you are not receiving a valid JSON string from the backend, it fails to parse it, and throws this parser error.

Edit: see jQuery documentation:

 dataType (default: Intelligent Guess (xml, json, script, or html))

Type: String The type of data that you're expecting back from the server.

From http://api.jquery.com/jquery.ajax/

Sign up to request clarification or add additional context in comments.

4 Comments

I've been eyeing that thinking it may have something to do with that but too inexperienced to be sure. Should I remove it or change it to something else?
Well.. this can be one of the resons... but since server code is hidden... and alert text is not clearly known... it can also be a parse-error on server side.
You should return a valid JSON from your PHP code ;-) If you have an object you want to return, just use json_encode() php.net/manual/fr/function.json-encode.php
Thank you so much! I was so fixated thinking it was a JS issue that I didn't even notice my problems in the PHP code. :)
0

Do not stringify the params before you pass them to your sendAction function.

Instead Stringify the entire object you send to the server. e.g.

data: JSON.stringify({ 'action': cmd, 'params': params }),

Comments

Your Answer

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