0

my json script is:

// add button .click
$('a.add').click(function(){ 
    $('#loader').show();
    var url = "/?"+$("form[name='jsms_add']").serialize();  
    ajx = $.ajax({
        url: url,
        type: 'post',
        data:{ajax:1},  
        dataType: 'json',
        success: function(r) {
            $('#loader').hide();
            if(r.r != 0){
                alert("ok");
                jsmsalert($('#alert_add'),'success',r.m);
                apendtable(r.r)
                $("tr").removeClass("odd");
                $("tr.viewrow:odd").addClass("odd");
                $("tr.editrow:odd").addClass("odd");
                $('td[colspan="7"]').remove();
            }
            else{
                jsmsalert($('#alert_add'),'error',r.m,0);                        
            }
        },
        error: function(request, status, err) {
            $('#loader').hide();
            jsmsalert($('#alert_add'),'error','error...'); 
            alert( "ERROR:  " + err + "  -  "  );
        }      
    }); 

and in my php file i have

$data = array(
    'r'    => '1',
    'm'    => '1',
);
json_encode($data);

now i want to know that how can i send a value to json that r.r != 0 be true and my success code execute? In Firefox the error is "SyntaxError: JSON.parse: unexpected character" this code used by another site by i don't know what happend in php file

my problem is that how can i send some data (for example r=1) to my json, because i want to check that if r=1 (means success) do something and show text from m (message) in my page

please help me to correct php file thank

6
  • 4
    I think you have a fundamental missunderstanding of javascript and php here. First, that is a javascript file, not json. JSON or Javascript Object Notation is used as a transfer type between client and server in this case. Second, you serialize the form in the url you are posting to, then manually set the data that you want to post? On the server side, you could need to echo any results you want the javascript to be able to read Commented Mar 31, 2014 at 19:25
  • Are you even sending the data from php to javascript? Commented Mar 31, 2014 at 19:33
  • header('Content-Type: application/json'); echo json_encode($data); Commented Mar 31, 2014 at 19:42
  • this code is not for me, but this code work in another site that i dont know what happend in php file, json code is in a file with .js extention (javascript) Commented Mar 31, 2014 at 19:45
  • I suggest you read more about what you're dealing with before you continue: JavaScript, JSON, PHP Commented Mar 31, 2014 at 20:02

1 Answer 1

1

In your php file make sure you are echoing the encoded data, like so:

$data = array(
'r'    => '1',
'm'    => '1',
);
echo json_encode($data);

If you don't echo the data it will not make it back to your JavaScript. If your JavaScript is what's throwing the errors try commenting out the dataType: 'json', and console logging the return. It might be php is throwing errors that jquery is refusing to parse.

$.ajax({'url': url,type: 'post',data:{ajax:1}, success: function(returnData){
        console.log(returnData);
}});
Sign up to request clarification or add additional context in comments.

5 Comments

yes i echo in my file but i have an error "SyntaxError: JSON.parse: unexpected character"
my last error for this code is SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
Let us know what gets logged in the console.
nothing showing in console just one time page that i sent "/?" shown
I don't understand please provide more data

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.