Extjs to call a RESTful webservice

Deadly 提交于 2019-12-11 02:51:35

问题


I am trying to make a RESTful webservice call using Extjs. Below is the code i am using:

Ext.Ajax.request({ url: incomingURL ,
   method: 'POST',
   params: {param1:p1, param2:p2},
   success: function(responseObject){
     var obj = Ext.decode(responseObject.responseText);
     alert(obj);
   },
   failure: function(responseObject){
     var obj = Ext.decode(responseObject.responseText);
     alert(obj);
   }
});

but it does not work, the request is sent using OPTIONS method instead of POST.

I also tried to do the same thing using below code but result is the same:

var conn = new Ext.data.Connection();
conn.request({
  url: incomingURL,
  method: 'POST',
  params: {param1:p1, param2:p2},
  success: function(responseObject) 
  {
    Ext.Msg.alert('Status', 'success');
  },
  failure: function(responseObject) 
  {
    Ext.Msg.alert('Status', 'Failure');
  }
});

But when i tried to do the same thing using basic ajax call ( using the browser objects directly i.e. XMLHttpRequest() or ActiveXObject("Microsoft.XMLHTTP")) it works fine and i get the response as expected.

Can anyone please help me, as i am not able to understand what i am doing wrong with extjs ajax call?


回答1:


You can't make a standard AJAX call between domains. The URL for Ext.Ajax.request should be a relative one (relative to the script's origin).

If you want to do cross-domain calls, use a ScriptTagProxy or such.




回答2:


The problem is exactly because of the reason ob1 and Chuck Hinson described.

I have an RESTful service, wich is running on Tomcat.

And i made a static client(no deployed to Tomcat) using ExtJs with Json reader. I just made an html page with ExtJs integrated consuming REST service like url: http://localhost:8080/service/invoices/

And all the time ExtJs was making OPTIONS request, not GET or POST even if i was setting them as being used methods. The problem is this security feature, because Client is not the part of same application and i am doing AJAX call between domains.

As soon as i put my client to my Web application and deployed to Tomcat and started using relative calls it started working.




回答3:


if you don't want cross-domain request, please remove the website prefix 'http://website' from propery url of ajax proxy.



来源:https://stackoverflow.com/questions/1826671/extjs-to-call-a-restful-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!