简体   繁体   English

具有JSON响应的跨域请求

[英]Cross domain request with JSON response

I am trying cross domain request from my js file. 我正在尝试从我的js文件跨域请求。

First,I was trying JSONP but my target domain URL is not support it. 首先,我正在尝试JSONP,但我的目标域URL不支持它。 It return plain JSON object. 它返回普通的JSON对象。

I am authorize person to access my target domain URL. 我授权人员访问我的目标域URL。 but i can not modify it as per JSONP response. 但我无法根据JSONP响应修改它。

SO how can i get JSON response from my target domain URL? 那我怎么能从我的目标域URL获得JSON响应?

I have always done it with jsonp, by passing a callback b/c services return json, if call back is passed then it will wrap all json in callback else they will simple return json. 我总是用jsonp完成它,通过传递一个回调b / c服务返回json,如果回调被传递,那么它将在回调中包装所有json,否则它们将简单地返回json。

But in your case 但在你的情况下

You can look up with this article http://www.webdevdoor.com/jquery/cross-domain-browser-json-ajax/ 你可以查阅这篇文章http://www.webdevdoor.com/jquery/cross-domain-browser-json-ajax/

Without modifying a bit the server side there is not much you can do. 如果不对服务器端进行一些修改,那么你就无能为力了。 The general policy is to not to allow cross domain requests. 一般策略是不允许跨域请求。

There are few things worth mentioning though: 但是值得一提的是:

  1. Try changing the server side so it will support JSONP. 尝试更改服务器端,以便它支持JSONP。
  2. If the HTTP response contains Access-Control-Allow-Origin header then you can communicate with it with normal AJAX. 如果HTTP响应包含Access-Control-Allow-Origin标头,那么您可以使用普通的AJAX与它进行通信。 This feature is supported in modern browsers only. 仅在现代浏览器中支持此功能。 Check this out for more info. 查看此信息以获取更多信息。
  3. You can do the cross domain requests with Flash and/or WebSockets. 您可以使用Flash和/或WebSockets执行跨域请求。 However server does have to support them. 但是服务器必须支持它们。

Don't know what type application you are developing. 不知道你正在开发什么类型的应用程序。 But in ASP.NET you can do it by using a proxy page 但是在ASP.NET中,您可以使用代理页面来完成它

These links may be helpful: 这些链接可能会有所帮助:

http://www.codeproject.com/Articles/667611/ASP-NET-Proxy-Page-Used-for-Cross-Domain-Requests http://www.codeproject.com/Articles/667611/ASP-NET-Proxy-Page-Used-for-Cross-Domain-Requests

http://encosia.com/use-asp-nets-httphandler-to-bridge-the-cross-domain-gap/ http://encosia.com/use-asp-nets-httphandler-to-bridge-the-cross-domain-gap/

https://gist.github.com/jkresner/3982746 https://gist.github.com/jkresner/3982746

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM