Annie
Annie

Reputation: 672

json data by ajax request is not been sent

I want to send json data to php file. when i send it as querystring it ,half of the data is sent and when i send it in the following way then nothing is sent at all

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

i have modified my ajax call in different ways but it always send null. I have checked that my hiddenfield 'jsonData' has data in it before making ajax request. please help here is json data--

{"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}]}

Upvotes: 1

Views: 1469

Answers (2)

Noname Provided
Noname Provided

Reputation: 225

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

That should work if you change those but perhaps remove

dataType: 'json',

aswell. Ive never used that and do not know if it exsists Also you cannot set charset it sends it as utf-8 reguardless of what you do

edit also log jsondata.val like the man above me said just to make sure

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

did you change your code to read like this? Also have you tried logging your failure error? if so what does it say. And you were missing a ; in your success function

Upvotes: 1

Travis J
Travis J

Reputation: 82337

I think you need to change your ajax call to this (asuming ("jsonData").value does contain a json object):

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

Read more here: http://joekuan.wordpress.com/2010/12/06/posting-json-data-from-ext-js-to-php/

Upvotes: 0

Related Questions