简体   繁体   中英

send data to json from php

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 rr != 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

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);
}});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM