1

I am using the following script to my php script on my server;

$.ajax({
            type: 'POST',
            url: 'http://xx.php',
            data: { anyNumber: val1 },
            success: function(data) {
                alert(data);
            }
});

the php script correct queries sqlsrv database and returns row of data that I encode with echo json_encode($rows); I then correctly receive the json string into data (as above example) When I display the data with alert(data); as above, I get the following:

"({\"id\":1,\"Username\":null,\"Category\":null,\"VIN\":null,\"Make\":null,\"Model\":null,\"Colour\":null,\"Registration\":\"12345     \",\"RegisterNumber\":null,\"EngineNo\":null,\"Branding\":null,\"Remarks\":null,\"Photo1\":null})"

I have tried various methods but cannot assign the various elements to variables. Please help

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
Harties
  • 25
  • 5
  • Is this your problem? http://stackoverflow.com/questions/6324614/php-why-do-you-escape-my-quotes – Ariel Feb 26 '16 at 18:54
  • I made the following changes: 1. inserted dataType 'json' as suggested. I have also changed the php script to individually populate the $rows array by element inside the loop. This resulted in a response of [object Object], [object Object], etc for each element in alert(data). I can now however correctly and easily access each element by assigning each element as a variable example: var tt = result[5].result; and I get the correct value. This works for me but not sure if this is the most appropriate approach. rgs – Harties Feb 28 '16 at 03:13

1 Answers1

2

just set the dataType of your $.ajax call to json:

$.ajax({
        type: 'POST',
        url: 'http://xx.php',
        dataType: 'json',
        data: { anyNumber: val1 },
        success: function(data) {
            alert(data);
        }
});

explanation: when delivering data with GET or POST a lot of characters are getting masked. I think your $.ajax call just doesn't expect JSON as an answer but text or html. that's why it's not reading the JSON string correctly and keeping the masking of the characters.

low_rents
  • 4,481
  • 3
  • 27
  • 55