如何从JavaScript调用REST API [英] how to call REST API from javascript
问题描述
我有一个给json数据的url ...
我想从javascript中找到这个URL,但我得到这个错误:
$ b未声明纯文本文档的$ b
字符编码。如果文档包含US-ASCII范围之外的字符,则该文档将在某些浏览器配置中呈现乱码文本。文件的字符编码需要在传输协议中声明,或者文件需要使用字节顺序标记作为编码签名。
代码:
function a(){
?
$ .getJSON(url,function(data){alert(data);});
$ / code $ / pre
$ b $完整的代码:
$ b< html lang =en>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>< / meta>
< script language =JavaScripttype =text / javascriptsrc =jquery-1.7.1.min.js>< / script>
< script>
function a(){
$ .getJSON(url,function(data){alert(data);});
}
< / script>
< / head>
< body>
< input type =text/>
< input type =submitvalue =searchonclick =a()/>
< / body>
< / html>
解决方案您的代码似乎正确。
$ b您是否在
完全限定URL调用
如果您正在制作一个完全合格的URL调用,确保以下内容。
- 您正在调用同一个域(相同的服务器)。您不能对另一个域进行
简单的JSON调用。
- 如果您想使用跨域调用,则必须使用JSONp
更新:
由于是跨域调用,因此无法使用。
解决此问题
JavaScript
创建一个函数
function getMyData(data){
alert(data);
//使用您的数据执行魔术
}
服务器side
在服务器端将数据包装在函数语法中
getMyData(在这里输入你的数据);
JavaScript
<然后创建一个脚本标记并添加一个链接到您的跨域页面。< script type =text / javascript
src =cross ref url>
< / script>
供参考: wikipedia
编辑:另一种选择是在您的域上创建代理。即在您的域中创建一个页面,该页面在内部调用跨域页面并将相同的数据返回给您的Ajax调用。
I have a url which gives json data...
I want to hit that URL from javascript but I am getting this error :
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
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>
解决方案Your code seems correct.
Are you making a
fully qualified URL call
?If you are making a fully qualified URL call, make sure of the following.
- You are calling the same domain(same server). You can not make a simple JSON call to another domain.
- If you want to use a cross domain call, you'll have to use 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.
这篇关于如何从JavaScript调用REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!