douqiao5543 2012-03-27 06:23
浏览 23
已采纳

将json数据发送到php web服务

I am sending to the server this call:

var Message = {};
Message['islands'] = '1-15-13';
Message['newMessageText'] = 'this is test message';
$.ajax({
    url: "sendnote.php",
    type: "POST",
    data: Message,
    dataType: "json",
    contentType: "charset=utf-8",
    success: function (data) {
        alert(data["result"]);
    },
    error: function (data) {
        alert(data["result"]);
    }
});

and on server (sendnote.php) I have

print_r($_POST);

just to check do I receive anything, but after cchecking response in Firebug I see that this array is empty.

What am I doing wrong in sending data?

Thanks in advance!

PS I've checked previous post on this subject, but still have problems with it.

  • 写回答

2条回答 默认 最新

  • douju2053 2012-03-27 08:03
    关注

    The problem is the contentType

    Try this:

    jQuery

    $(document).ready(function(){
    var Message = {};
        Message['islands'] = "1-15-13";
        Message['newMessageText'] = 'this is test message';
    
    $.ajax({
            url: "sendnote.php",
            type: "POST",
            data: Message,
            dataType: "json",
            success:function(data) {
                alert("Islands: "+data.islands+", Message: "+data.newMessageText);
                },
            error:function(data) {
                alert('error');
            }  });
    });
    

    php

    <?php
        echo json_encode($_POST);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题