如何从javascript调用REST API

时间:2022-10-16 13:13:02

I have a url which gives json data...

我有一个提供json数据的网址...

I want to hit that URL from javascript but I am getting this error :

我想从javascript中点击该URL但我收到此错误:

character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature

未声明纯文本文档的字符编码。如果文档包含US-ASCII范围之外的字符,则文档将在某些浏览器配置中使用乱码文本进行渲染。需要在传输协议中声明文件的字符编码,或者文件需要使用字节顺序标记作为编码签名

Code :

function a(){
$.getJSON(url,function(data) { alert(data);});
}

full code :

完整代码:

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" ></meta>
<script language="JavaScript" type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script>

function a(){
$.getJSON(url,function(data) { alert(data);});
}
</script>
</head>
<body>
<input type="text"/>
<input type="submit" value="search" onclick="a()"/>
</body>
</html>

1 个解决方案

#1


10  

Your code seems correct.

你的代码似乎是对的。

Are you making a fully qualified URL call?

您是否正在进行完全限定的URL呼叫?

If you are making a fully qualified URL call, make sure of the following.

如果您要进行完全限定的URL呼叫,请确保以下内容。

  1. You are calling the same domain(same server). You can not make a simple JSON call to another domain.
  2. 您正在调用相同的域(相同的服务器)。您无法对另一个域进行简单的JSON调用。

  3. If you want to use a cross domain call, you'll have to use JSONp
  4. 如果要使用跨域调用,则必须使用JSONp

Update: This is not working since it is a cross domain call.

更新:这不起作用,因为它是跨域调用。

Work around for this

解决这个问题

JavaScript

Create a function

创建一个功能

function getMyData(data) {
    alert(data);
    //Do the magic with your data
}

Server side

On server end wrap your data inside function syntax

在服务器端将数据包装在函数语法中

getMyData("Enter your data here");

JavaScript

Then create a script tag and add a link to your cross-domain page

然后创建一个脚本标记并添加一个指向跨域页面的链接

 <script type="text/javascript"
         src="cross ref url">
 </script>

For reference: wikipedia

供参考:*

EDIT: Another option is Create a proxy on your domain. ie create a page in your domain which internally calls the cross-domain page and return the same data to your Ajax call.

编辑:另一个选项是在您的域上创建代理。即在您的域中创建一个页面,该页面在内部调用跨域页面并将相同的数据返回给您的Ajax调用。

#1


10  

Your code seems correct.

你的代码似乎是对的。

Are you making a fully qualified URL call?

您是否正在进行完全限定的URL呼叫?

If you are making a fully qualified URL call, make sure of the following.

如果您要进行完全限定的URL呼叫,请确保以下内容。

  1. You are calling the same domain(same server). You can not make a simple JSON call to another domain.
  2. 您正在调用相同的域(相同的服务器)。您无法对另一个域进行简单的JSON调用。

  3. If you want to use a cross domain call, you'll have to use JSONp
  4. 如果要使用跨域调用,则必须使用JSONp

Update: This is not working since it is a cross domain call.

更新:这不起作用,因为它是跨域调用。

Work around for this

解决这个问题

JavaScript

Create a function

创建一个功能

function getMyData(data) {
    alert(data);
    //Do the magic with your data
}

Server side

On server end wrap your data inside function syntax

在服务器端将数据包装在函数语法中

getMyData("Enter your data here");

JavaScript

Then create a script tag and add a link to your cross-domain page

然后创建一个脚本标记并添加一个指向跨域页面的链接

 <script type="text/javascript"
         src="cross ref url">
 </script>

For reference: wikipedia

供参考:*

EDIT: Another option is Create a proxy on your domain. ie create a page in your domain which internally calls the cross-domain page and return the same data to your Ajax call.

编辑:另一个选项是在您的域上创建代理。即在您的域中创建一个页面,该页面在内部调用跨域页面并将相同的数据返回给您的Ajax调用。