Ext.Ajax.request POST问题

时间:2013-05-30 18:54:07

标签: jquery ajax json extjs

使用Ext.Ajax.request POST到URL时,我注意到了奇怪的行为。

我有以下Ext.Ajax.request:

var url = url_posted_to.org,
    obj = 
    {
            "evaluation": {
                "evaluation_id":44216,
                "garden_id":37288,
                "eval_type":1,
                "score":15,
                "rating":"GD",
                "rating_year":2013,
                "best_of":"NADA!",
                "special_award_specified":null,
                "evaluator_id":"265",
                "nate_siegel_award":0,
                "rainbarrel":0,
                "date_evaluated":"1969-12-31T18:00:00",
                "comments":"",
                "scoresheet":{
                    "color":1,
                    "plant_variety":2,
                    "design":3,
                    "maintenance":4,
                    "environmental_stewardship":5
                }
            },
            "garden":{
                "garden_id":37288,
                "name":"Larry Opelt",
                "no_longer_exists":0,
                "raingarden":0
            },
            "geolocation":{
                "latitude":44.9615709,
                "longitude":-93.3353673,
                "accuracy":25
            }
        };   



Ext.Ajax.request({

        method: "POST",
        url: url,
        params:  obj,
        success: function (response) {
            alert('success: ' + response.responseText);

        },
        failure: function (e, jqxhr) {
            alert('failure!');
            alert(e.status);
        }
    }); 

我收到错误:POST url_posted_to.org 500(内部服务器错误)。

表单数据的响应标题:

Form Dataview sourceview URL encoded
evaluation:44216
evaluation:37288
evaluation:1
evaluation:15
evaluation:GD
evaluation:2013

evaluation:NADA!
evaluation:
evaluation:265
evaluation:0
evaluation:0
evaluation:1969-12-31T18:00:00
evaluation:
evaluation:[object Object]

garden:37288
garden:Larry Opelt
garden:0
garden:0
geolocation:44.9615709
geolocation:-93.3353673
geolocation:25

然而......这是奇怪的部分......如果使用jQuery的Ajax实现提出相同的请求,一切都很好。

这是jQuery Ajax调用:

$.ajax({
    type: 'POST',
    url: url,
    data: params,
    success: function(data, textStatus, jqXHR){
        if(data.result == 'success'){
        $('#results').html('RESULTS:<br>');
        $('#results').append(JSON.stringify(data, null, '<BR>'));
    } else {
        $('#results').html('ERROR:<br>');
            $('#results').append(JSON.stringify(data));
    }
    },
    error: function(jqXHR, exception, errorThrown  ){
        $('#results').html('ERROR:<br>');
    $('#results').append(jqXHR.status);
    },
    complete: function(){
        alert("ajax complete: "+data.result)
    },
});

响应头(看起来非常好):

evaluation[best_of] NADA!
evaluation[comments]    
evaluation[date_evaluated...    1969-12-31T18:00:00
evaluation[eval_type]   
evaluation[evaluator_id]    265
evaluation[nate_siegel_aw...    0
evaluation[rainbarrel]  0
evaluation[rating]  GD
evaluation[rating_year] 2013
evaluation[score]   15
evaluation[scoresheet][co...    1
evaluation[scoresheet][de...    3
evaluation[scoresheet][en...    5
evaluation[scoresheet][ma...    4
evaluation[scoresheet][pl...    2
evaluation[special_award_...    
garden[name]    Larry Opelt
garden[no_longer_exists]    0
garden[raingarden]  0
geolocation[accuracy]   25
geolocation[latitude]   44.9615709
geolocation[longitude]  -93.3353673

因此,看起来Ext.Ajax正在使用嵌套的JSON做一些时髦的事情。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

尝试使用jsonData参数代替params

Ext.Ajax.request({    
        method: "POST",
        url: url,
        jsonData:  obj,
        success: function (response) {
            alert('success: ' + response.responseText);
        },
        failure: function (e, jqxhr) {
            alert('failure!');
            alert(e.status);
        }
    }); 

答案 1 :(得分:1)

您还可以坚持使用params并致电Ext.JSON.encode(obj)

Ext.Ajax.request({

    method: "POST",
    url: url,
    params:  Ext.JSON.encode(obj),
    success: function (response) {
        alert('success: ' + response.responseText);

    },
    failure: function (e, jqxhr) {
        alert('failure!');
        alert(e.status);
    }
});

我尝试使用jsonData(这绝对适用于简单的json对象),但发现它没有正确编码嵌套在我的json对象中的数组。