如何在sencha touch中跨域发布JSON请求 [英] How to POST JSON request in cross domain in sencha touch

查看:96
本文介绍了如何在sencha touch中跨域发布JSON请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网址

https://development.avalara.net/1.0/tax/get

,并希望在JSON请求正文之后进行发布

and would like to POST following JSON request body

{ 
"DocDate": "2011-05-11", 
"CustomerCode": "CUST1", 
"Addresses": 
[ 
{ 
"AddressCode": "1", 
"Line1": "435 Ericksen Avenue Northeast", 
"Line2": "#250", 
"PostalCode": "98110" 
} 
]
}

然后将给出JSON响应

which then will give JSON response

{ 
"DocCode": "78b28084-8d9a-477c-9f26-afab1c0c3877", 
"DocDate": "2011-05-11", 
"Timestamp": "2011-05-11 04:26:41", 
"TotalAmount": 10, 
"TotalDiscount": 0, 
"TotalExemption": 0, 
"TotalTaxable": 10, 
"TotalTax": 0.86, 
"TotalTaxCalculated": 0.86, 
"TaxDate": "2011-05-11",
.......
}

我尝试使用

Ext.Ajax.request

Ext.Ajax.request

但出现错误

Origin http://localhost is not allowed by Access-Control-Allow-Origin.

可能是由于具有不同的域.

which might be due to having different domain.

所以,然后我尝试使用JSONP

So, then i tried to use JSONP

Ext.data.JsonP.request
(
{
url: 'https://development.avalara.net/1.0/tax/get',
callbackName: 'test',
method: 'POST',
jsonData: '{"DocDate": "2011-05-11", "CustomerCode": "CUST1", "Addresses": [ { "AddressCode": "1", "Line1": "435 Ericksen Avenue Northeast","Line2": "#250", "PostalCode": "98110" } ] }' ,
success: function(response) {
//do some successful stuff
Ext.Msg.alert(response);
},
failure: function(response) {
//complain
Ext.Msg.alert('fail');
}
});

但是遇到URL 404(未找到)错误,请求方法是GET而不是POST.

But URL 404(Not Found) error is encountered and request method is GET instead of POST.

任何人都可以帮助我如何通过POST请求正文(JSON)并从不同域获得JSON响应吗?

Can anyone help me how POST request body(JSON) and obtaind JSON response from different domain?

预先感谢

推荐答案

您有四个选择:

  1. 使用 CORS . development.avalara.net需要在服务器上设置CORS,并允许运行Sencha页面的域.

  1. Use CORS. development.avalara.net would need to setup CORS on the server and allow the domain that the Sencha page is running on.

通过运行Sencha页面的域上的服务器的反向代理请求:

Reverse Proxy requests through a server on the domain that the Sencha page is running on:

Sencha页面(mydomain.com)---> Web服务器(mydomain.com)---> development.avalara.net
Sencha页面(mydomain.com)< --- Web服务器(mydomain.com)< --- development.avalara.net

Sencha page (mydomain.com) ---> Web Server (mydomain.com) ---> development.avalara.net
Sencha page (mydomain.com) <--- Web Server (mydomain.com) <--- development.avalara.net

您还可以将表单发布为常规表单发布操作,或在隐藏的iframe中发布表单.

You could also POST the form as a regular form post action, or POST the form inside a hidden iframe.

http://docs .sencha.com/extjs/4.2.1/#!/api/Ext.form.Basic-cfg-standardSubmit

在phonegap/cordova中运行Sencha应用程序,该应用程序不会阻止跨域请求.

Run the Sencha app inside phonegap/cordova which does not block cross-domain requests.

这篇关于如何在sencha touch中跨域发布JSON请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆