最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - how to call REST API from javascript - Stack Overflow

programmeradmin2浏览0评论

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>

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>
Share Improve this question edited Jan 21, 2013 at 6:22 Rishi asked Jan 21, 2013 at 6:13 RishiRishi 1,3493 gold badges22 silver badges40 bronze badges 6
  • Where does the error appear? – Quentin Commented Jan 21, 2013 at 6:19
  • In this js : jquery-1.7.1.min.js – Rishi Commented Jan 21, 2013 at 6:19
  • this is more of a problem with character encoding set by the document interpreting your request. Who controls the server side code for this? – Brian Vanderbusch Commented Jan 21, 2013 at 6:21
  • which URL do you want to load via AJAX? – user1386320 Commented Jan 21, 2013 at 6:23
  • @Brian Vanderbusch. Heroku – Rishi Commented Jan 21, 2013 at 6:25
 |  Show 1 more comment

1 Answer 1

Reset to default 11

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.

  1. You are calling the same domain(same server). You can not make a simple JSON call to another domain.
  2. 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.

发布评论

评论列表(0)

  1. 暂无评论