Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting incorrect response in JsonP

I am sending data from index.php file to another exec.php file using $.ajax method and executing the data on that file now when i am sending back output data using jsonp callback i am getting incorrect data on index.php. I am also writing same output data into a txt file on exec.php and i am getting correct data in txt file.

here's my ajax call from index.php

$.ajax({
            type: 'GET',
            url: 'exec.php',
            dataType: 'JSONP',
            data: {code : code},
            success: function(data) 
            {
                alert(data);
                $(loader).addClass('hidden');
                var stdout = $(form).children('.stdout');
                if (data.search("Parse error")>0)
                {
                    var str = data.replace('<b>Parse error</b>:  ','');
                    $(stdout).html(str);
                    $(stdout).removeClass('hidden');    
                }   
                else
                {
                    $(stdout).html(data);
                    $(stdout).removeClass('hidden');
                }   
            },
             error: function (status,req,err) { 
             var errorMessage = err || req.statusText;
                alert(errorMessage);
             }, // When Service call fails             

            responseType: "jsonp"
        });   

Here's my json callback code from exec.php

$result = str_replace('"','\"', $script_output);
$script_output = str_replace('"','\"', $script_output);
$file = fopen("test.txt","w");
echo fwrite($file,$script_output);
fclose($file);
//print "processJSON({'result':'$result'})";
header('Content-Type: application/jsonp');
echo $_GET['callback'] . '(' . "{'result' : '$result'}" . ')';

Now here's the response i am getting in firebug.

Screenshot - http://screencast.com/t/nqdAR7JedMk I'm getting this error by alert through error of ajax method Screenshot - http://screencast.com/t/5RRlYiha2C0

Please suggest me what i am getting wrong


2 Answers

i have find my answer. The extra numbers is return with json call back. so i have commented out and my some code and my perfect output is here,

$result = str_replace('"','\"', $script_output);
$script_output = str_replace('"','\"', $script_output);
//$file = fopen("test.txt","w");
//echo fwrite($file,$script_output);
//fclose($file);
//print "processJSON({'result':'$result'})";
header('Content-Type: application/jsonp');
echo $_GET['callback'] . '(' . "{'result' : '$result'}" . ')';

There is something in your code that prints number (2311 on your screenshot) before the line print "processJSON({'result':'$result'})";.

That number before identifier makes javascript code invalid.

like image 43
dm0_ Avatar answered Jun 15 '26 07:06

dm0_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!