11

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>
6
  • Where does the error appear? Commented Jan 21, 2013 at 6:19
  • In this js : jquery-1.7.1.min.js 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? Commented Jan 21, 2013 at 6:21
  • which URL do you want to load via AJAX? Commented Jan 21, 2013 at 6:23
  • @Brian Vanderbusch. Heroku Commented Jan 21, 2013 at 6:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

you mean I must use url as var url=url+"?callback=?" I have used that..I didn't find any result....
No. Can you post the full vale of "url"
Simply use url = 'getAllJobs' and try
Are you calling it from the same domain (ie a page in scalajobz.com itself)?
Nothing happen...How can this url="getAllJobs" work without giving full URL..because I am using this URL in my another application...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.