jQuery 带有 JSON 响应的跨域请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19652005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:22:47  来源:igfitidea点击:

Cross domain request with JSON response

javascriptjquery

提问by Rickyrock

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. It return plain JSON object.

首先,我正在尝试 JSONP,但我的目标域 URL 不支持它。它返回纯 JSON 对象。

I am authorize person to access my target domain URL. but i can not modify it as per JSONP response.

我被授权访问我的目标域 URL。但我无法根据 JSONP 响应修改它。

SO how can i get JSON response from my target domain URL?

那么如何从目标域 URL 获得 JSON 响应?

回答by developerCK

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/

回答by freakish

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

回答by Shibbir Ahmed

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