dse3168 2014-03-31 19:22
浏览 37

从php发送数据到json

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

  • 写回答

1条回答 默认 最新

  • douying0108 2014-03-31 20:16
    关注

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测