将JSON数据发送到php Web服务 [英] Sending json data to php web service

查看:142
本文介绍了将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"]);
    }
});

并且在服务器(sendnote.php)上我有

and on server (sendnote.php) I have

print_r($_POST);

只是为了检查我是否收到了任何东西,但是在Firebug中检查了响应之后,我看到此数组为空.

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?

提前谢谢!

我已经检查了有关此主题的上一篇文章,但是仍然有问题.

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

推荐答案

问题是contentType

The problem is the contentType

尝试一下:

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

这篇关于将JSON数据发送到php Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆