我正在尝试通过ajax请求创建一个SharePoint应用程序,通过AJAX请求使用在另一个网站/ Web应用程序上生成的JSON数据。让我们说我想要从另一个网站获得的JSON数据是一些笔记本电脑品牌的列表。

SharePoint App / Environm在另一个服务器上,而不是笔记本电脑品牌的网站,笔记本电脑网站以任何方式与SharePoint无关。假设以下内容:

var laptopUrl = "https://examplewebsite.com/laptopbrands"; // returns a json list with laptop brands
var sharepointWebsite = 'https://totallydifferentwebsite.com/sharepoint';
.

当在浏览器中导航到Laptopurl时,它输出JSON数据,但在SharePoint应用程序中使用代码时,不会返回数据。我尝试使用以下代码(以及其他几个jQuery Ajax请求):

$.getJSON( laptopUrl, function( data ) {
    console.log(data); // Empty
});
.

我一直在尝试在SharePoint应用程序中获取JSON数据,而是没有成功......我看到了一个雅虎天气app 这几乎是我想要的,但是当我将URL改为我自己的JSON输出时,结果是空的...

我做错了什么,或者是我想要实现的东西根本无法实现吗?

提前感谢,

Ilians

有帮助吗?

解决方案

Looks like you need JSONP in this scenario:

JSONP or "JSON with padding" is a communication technique used in JavaScript programs which run in Web browsers. It provides a method to request data from a server in a different domain, something prohibited by typical web browsers because of the same origin policy.

JSONP could be triggered using:

$.getJSON(laptopUrl + "?callback=?", null, function(data) 
{
    console.log(data); 
});

The addition of '?callback=?' will trigger a jsonp request.

许可以下: CC-BY-SA归因
scroll top