没有发送ajax请求的json数据

时间:2012-12-24 06:51:07

标签: php ajax jquery extjs

我想将json数据发送到php文件。当我发送它作为查询字符串时,一半数据被发送,当我以下列方式发送它时,根本没有发送任何内容

 Ext.Ajax.request({
        url: 'GetData.php',
         params: {
            data:document.getElementById("jsonData").value

          },
       method: "POST",

        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(xhr) {
       console.log(xhr)
        }
 });

我以不同的方式修改了我的ajax调用,但它总是发送null。在发出ajax请求之前,我已经检查过我的hiddenfield'jsonData'中有数据。请帮忙  这是json数据 -

{"items":[{"text":"Table of Contents","items":[{"text":"Cover","source":"book/00__Cover.html","leaf":true,"items":"[]"},
{"text":"Introduction","source":"book/Introduction.html","leaf":true,"items":"[{\"text\":\"Me maps\",\"source\":\"book/Introduction.html#c000030\\\"\",\"leaf\":true},{\"text\":\"Spatial perspective\",\"source\":\"book/Introduction.html#c000031\\\"\",\"leaf\":true}]"},{"text":"Index","source":"book/Index.html","leaf":true,"items":"[]"}]},{"text":"My Study Guide","source":"studyguide.js","leaf":true},{"text":"Shared","source":"shared.js","leaf":true}]}

2 个答案:

答案 0 :(得分:1)

    headers: { 'Content-Type': 'application/json' }, 
    jsonData: {
        document.getElementById("jsonData").value
    },

dataType: 'json',

以及。我从未使用过它,也不知道它是否存在 此外,你不能设置charset它发送它作为utf-8,无论你做什么

编辑也记录jsondata.val就像上面的那个男人说的那样只是为了确保

<强> EDIT2

 Ext.Ajax.request({
        url: 'GetData.php',
        headers: { 'Content-Type': 'application/json' }, 
        jsonData: {
            document.getElementById("jsonData").value
        },
        method: "POST",
        dataType: 'json',
        success: function(xhr) {
            console.log(xhr);
        }
 });

你改变你的代码是这样的吗?您是否尝试过记录失败错误?如果是这样的话呢。你错过了一个;在你的成功功能

答案 1 :(得分:0)

我认为您需要将ajax调用更改为此(假设("jsonData").value包含json对象):

Ext.Ajax.request({
    url: 'GetData.php',
    jsonData: Ext.util.JSON.encode(document.getElementById("jsonData").value)
    method: "POST",
    success: function(xhr) {
      console.log(xhr)
    }
});

在此处阅读更多内容:http://joekuan.wordpress.com/2010/12/06/posting-json-data-from-ext-js-to-php/

相关问题