【问题标题】:Getting data from web service using ajax使用 ajax 从 Web 服务获取数据
【发布时间】:2014-02-20 09:15:01
【问题描述】:

我正在尝试使用 ajax 和 POST 方法从网络服务获取数据。

Ext.Ajax.request({
url: 'http://localhost......',
method:'POST',
   success: function(response) { console.log(response.responseText); },
  failure: function() { Ext.Msg.alert('Fail'); },
  jsonData:{ 
      /*Here I specify my request json*/
    }
}
  });

上面的东西工作正常,但是当我尝试在 EXTJS 存储中模仿它时,它会响应错误 服务器响应状态为 415(不支持的媒体类型)

EXTJS 商店代码

Ext.define('IWM.store.JsonTest', {

    extend: 'Ext.data.Store',
    autoLoad: true,

    fields:['Name'],

    proxy: {

        type: 'ajax',
        method : 'POST',
        actionMethods: {
            create : 'POST',
            read   : 'POST',
            update : 'POST',
            destroy: 'POST'
        },
        jsonData:{
            /*JSON */
            }
        },
        url: 'http://localhost......',

        success: function(response) { console.log(response.responseText); },

        failure:function(){console.log("failed");},

        reader: {

            type: 'json',

            root: 'result',

            successProperty: 'success'

        }

    }

});

【问题讨论】:

  • 你的网络服务器是什么?阿帕奇或IIS!另外,发布您的服务器端代码,特别是返回值部分。
  • 服务器是 apache。服务器正确地为我提供了一个 json。

标签: ajax extjs extjs4.1 extjs4.2 extjs-mvc


【解决方案1】:

首先,尝试如下更改商店配置。

var IWMStore = new Ext.data.JsonStore({
   // it seems to me you had forgotten model
    model: 'YourDataModel',
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url: 'http://your-url-address',
        reader: {
            type: 'json',
            root: 'json-root-value',
            idProperty: 'unique-property-in-json-file'
        }
    }
});

JSON 值示例:

// here, ctgMains is the root value, CUST_ASSORT_SECTION_ID is the idProperty
{"ctgMains":[{"CUST_ASSORT_SECTION_ID":"1","CTG_SECTION":"HORECA"},{"CUST_ASSORT_SECTION_ID":"7","CTG_SECTION":"SCO"},{"CUST_ASSORT_SECTION_ID":"3","CTG_SECTION":"TRADER"}]}

第二次尝试: 尝试在您的代理定义中添加headers 属性,如下所示:

        proxy: {
            type: 'ajax',
            url: 'http://your-url-address',
            reader: {
                type: 'json',
                root: 'json-root-value',
                idProperty: 'unique-property-in-json-file'
            },
            headers: {
                'Content-type': 'application/json',
                'Accept': 'application/json'
            }
        }

【讨论】:

  • 我仍然收到同样的错误“不支持的媒体类型”
  • 发布您返回的 json 和模型定义。
  • No :( 不工作错误仍然存​​在。我什至收到错误指定“没有找到匹配请求路径/ActionflowResponse 的操作,HTTP 方法:POST,ContentType:application/json,application/ x-www-form-urlencoded;charset=UTF-8, Accept : application/json'
  • 您确定在浏览器控制台中看到 json 值吗?如果是这样,请将此 json 粘贴到此处。
  • 只有当我使用 Ext.Ajax.Request 方法时,我才能看到 json。但在商店中我无法在控制台中看到 json
【解决方案2】:

根据 Oğuz Çelikdemir 的回答,您可能需要为您的代理定义一个 json 编写器。

proxy: {
    type: 'ajax',
    url: 'http://your-url-address',
    reader: {
        type: 'json',
        root: 'json-root-value',
        idProperty: 'unique-property-in-json-file'
    },
    writer: {
        type: 'json'
    }
}

这应该设置 json mediatype 标头(假设这是您的请求所期望的)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2020-05-25
    • 2023-03-17
    • 2018-04-24
    相关资源
    最近更新 更多