Uncaught syntax error: Unexpected token < , ajax call - javascript

<script>
(function(){
var searchURL = 'http://en.wiktionary.org/wiki/search';
$.ajax({
type: "GET",
url: searchURL,
dataType: "jsonp",
cache: false,
async:false,
success: function(responseData, textStatus, XMLHttpRequest){
iframe(responseData);
}
});
})();
</script>
I added this script to my html file and it is show the following errors, copy pasting the function in console is also showing the same errors.
Uncaught SyntaxError: Unexpected token <
Resource interpreted as Script but transferred with MIME type text/html
could anyone help me resolve this issue, I am using Chrome brower.

You can't request arbitrary pages via AJAX, and jsonp doesn't magically make that work. You need to use the Wiktionary API.
The URL is http://en.wiktionary.org/w/api.php.
$.ajax({
url: 'http://en.wiktionary.org/w/api.php',
dataType: 'jsonp', // will automatically add "?callback=jqueryXXX"
cache: true, // the API complains about the extra parameter
data: { // the parameters to add to the request
format: 'json',
action: 'query',
titles: 'test'
},
success: function(data){
console.log(data);
}
});

Related

Ajax get with jsonp gives "SyntaxError: missing ; before statement" error

I am using jQuery for GET request and getting a response in json format. But when I try to use it, it throws below error message in browser developer tools console.
SyntaxError: missing ; before statement[Learn More] hosts:1:10 >>
Below is my code.
$.ajax({
type: "GET",
url: url,
async: false,
cache: false,
data: { "filter": "host.vars.osgroup==\"unix\""},
jsonp: "callback",
dataType: "jsonp",
contentType: "application/json; charset=utf-8",
headers: {
accept:'application/json',
"Authorization": "Basic " + btoa(username + ":" + password)
}
// },
// success : function(data)
// {
// console.log(data);
// }
})
.done(function(html) {
$("#displayElement").append(html);
});
This error occures just when the response of the server is not in valid JSONP format. To clear the things on you JSONP is not json. it is javascript script you can say. If you are using say Php then your php should output like this:
header("Content-Type: application/json");
exit("mycallback(".json_encode(['error' => 0,'auth_token' => $_COOKIE['server_token']]).")");
You see that Php code is outputting a string which has javascript function call with json formatted data as parameter. Now in ajax call you should have everything stream-lined. Ajax call should be like this:
$.ajax({
url:'http://ssoidpclient.local/get_auth_token',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'mycallback',
success:function(data)
{
}
});

Uncaught SyntaxError: Unexpected token u with Ajax post

I am stumped as to how to solve/diagnose ajax/jquery error.
This is my function:
var LogIn = {
Email: $("#Name").val(),
MobileNo: $("#txtMobileNumber").val(),
PinCode: '',
Message: '',
Success:false
};
$.ajax({
type: "POST",
crossDomain: true,
dataType: 'jsonp',
contentType: "application/json",
url: "https://a different server domain/api/LoginRequest",
data: JSON.stringify(LogIn),
success: function (data) {
$("#divError").html(data);
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
$("#divError").html(jsonValue);
}
});
I get this error:
jQuery doesn't support using POST and jsonp and the reason for that is very simple: when you inject a <script> tag into the DOM (which is what jQuery does when you use jsonp), the browser will send a GET request to the remote endpoint which has been referred to in the src property of this tag.
So basically you will need to use GET instead:
type: "GET"
Also since the data is sent as query string parameters you should remove the content type:
contentType: "application/json",
and do not JSON.stringify the data.
And here's the full example:
$.ajax({
type: "GET",
crossDomain: true,
dataType: 'jsonp',
url: "https://a different server domain/api/LoginRequest",
data: LogIn,
success: function (data) {
$("#divError").html(data);
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
$("#divError").html(jsonValue);
}
});
Of course this will work only if the remote endpoint supports JSONP and the GET verb.
Personally I would recommend using CORS instead of JSONP as it would give you much more options. You will be able to use POST in this case. Please refer to the following material as it seems you are using ASP.NET Web API on the server and trying to make a cross domain AJAX call: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

IE8: Access denied

I am trying to access and API using jquery/post but its not working in IE8. Its throwing Access denied error in IE8 only.
js code:
var url = 'http://somecomp.cartodb.com:80/api/v1/map?map_key=xxxxxxxxxxxxxxxxxxxx&stat_tag=API';
var data = //some long data of length greater than 3000
$.ajax({
crossOrigin: !0,
type: "POST",
method: "POST",
dataType: "json",
contentType: "application/json",
url: url,
data: JSON.stringify(data),
success: function(a) {
console.log('success');
},
error: function(a) {
console.log('error');
}
})
If I add ?callback=? at the end of url, it still fires the error callback but with statusText: 'success' and code: 200
here is full code: http://textuploader.com/ato0w
Change dataType to jsonp will allow you to make cross-domiain requests. This will work only with GET requests.
If you're using CORS for accessing cross-origin resource, try to add the following line:
$.ajax({
crossDomain: true, // replace "crossOrigin: !0;"
});
If this not working for you, try to add the following line above $.ajax() call.
jQuery.support.cors = true;

jquery ajax request with json throwing parsererror

I have searched lot of on this issue and tried almost everything but it doesn't seem to be work so posting it here.
I am trying to make jquery ajax call to ashx handler by passing json data. But my request doesnt reach till handler GetBalance.ashx
code :
var mydata = {};
$.ajax({
url: 'Handlers/GetBalance.ashx?type=authenticateRequest',
type: 'post',
dataType: 'json',
cache: false,
data: mydata,
success: function (data) {
},
error: function (xhr, status) {
console.log(status);
console.log(xhr.responseText);
}
});
in console it prints
parsererror
(empty string)
What i am doing wrong ? It should reach till .ashx before it gives any response parse error
Edit:
Changed as suggested by answer
Replace your line
data: mydata,
with
data: JSON.stringify(mydata),
contentType: "application/json",

Calling mtgox API gives JS error: Unexpected token :

I get the following error when I try to get info from the mtgox API. In chrome I can see that using jsonp is working, and there's no cross domain errors:
$.ajax({
type: 'GET',
url: 'https://mtgox.com/api/1/BTCUSD/ticker?callback=?',
async: false,
contentType: 'application/json',
dataType: 'jsonp',
success: function(json) {
console.log('ok');
}
});

Categories