【问题标题】:Get response text from a JSONP request in Ext / Java Application从 Ext / Java 应用程序中的 JSONP 请求中获取响应文本
【发布时间】:2023-03-07 00:14:01
【问题描述】:

我正在尝试将 Java REST 服务连接到 ExtJs JSONP 请求,但即使执行了 java 方法,我也无法获得响应测试。这就是我正在尝试的:

Java 代码:

@Path("/hello")
public class Hello {

      @GET
      @Produces("text/javascript") // have also tried application/json
      public String sayJsonHello(@QueryParam("_dc") String dcIdentifier, @QueryParam("callback") String callback) {
          System.out.println(callback);
          callback += "({\"success\":true, \"msj\":" + "\"" + "Exitoooo!" + "\" });";
          System.out.println(callback);
        return callback;
      }

}

ExtJs 代码:

    Ext.data.JsonP.request({
        url: "http://10.1.50.66:7001/Simulador/webresources/hello",
        params: {
        },
        callback: function (response) {
            console.log(response); //true
            console.log(response.result); //undefined
            console.log(response.responseText); //undefined
            console.log(response.success); // undefined
            if (response.success === true) {
                Ext.Msg.alert('Link Shortened', response.msj, Ext.emptyFn);
            } else { // entering here :( why ?
                Ext.Msg.alert('Error', response.msj, Ext.emptyFn);
            }
        }
    });

响应打印为真,其他一切未定义:(

回调看起来像这样Ext.data.JsonP.callback1({"success":true, "msj":"exito"})

有什么想法可能是错的吗?

【问题讨论】:

    标签: java javascript json extjs jsonp


    【解决方案1】:

    好的,这对我有用:

        Ext.data.JsonP.request({
            url: "http://10.1.50.66:7001/Simulador/webresources/hello",
            callbackKey: 'callback1',
            params: {
            },
            success : function(response) {
                console.log("Spiffing, everything worked");
                // success property
                console.log(response.success);
                // result property
                console.log(response.result);
                console.log(response.msj);
             },
             failure: function(response) {
                  console.log(response);
                  Ext.Msg.alert('Error', 'Please try again.', Ext.emptyFn);
              }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 2012-11-18
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      • 2012-07-11
      相关资源
      最近更新 更多