This link gets some random JSON data. To the best of my knowledge, it is valid.
However, when I try to get it using this code:
$.ajax({
type: "GET",
url: "https://getquote.herokuapp.com/get",
dataType: "jsonp",
success: function(data){
console.log("getting data...")
console.log(data);
},
error: function(err){
console.log("ran into an error...");
}
});
... I get an error that says, "Unexpected token :"
Related
I'm Use Laravel 5.2 And Ajax For Crud
Insert to Database is Correct But When Laravel Response The Browser Show Below Error In Console
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Laravel Code:
return response()->json('ok');
Jquery Code:
$.ajax({
type: type,
url: my_url,
data: formData,
dataType: 'json',
success: function (data) {
....
error: function(data){// Error...
var errors = $.parseJSON(data.responseText); console.log(errors);
$.each(errors, function(index, value) { $.gritter.add({
title: 'Error',
text: value
});
});
}
RedyState=4
Staus = 200
Can you paste your all controller code related to this error? Make sure you're not echoing anything before returning json response.
For example,
Route::get('/', function () {
echo "hi";
return response()->json('ok');
});
It would cause parse error.
With the Json response, what if you try something like.
return response()->json(['status'=>'ok']);
You javascript code should be
$.ajax({
type: type,
url: my_url,
data: formData,
dataType: 'json',
success:function (xhr){
var data = xhr.data;
},
error: function (error){
}
});
And why are you using var errors = $.parseJSON(data.responseText); ? Your response will be always json, no?
I need to get the value for #odata.context property from the following Json response in ajax:
{
"#odata.context":"https://site.sharepoint.com/_api/",
"#odata.type":"#oneDrive.permission",
"#odata.id":"https",
"link":{"scope":"anonymous"}
}
I would like to do something like that in code:
$.ajax({
type: "POST",
beforeSend: function (request) {
request.setRequestHeader("Authorization", 'Bearer ' + bearerToken);
},
url: serverUrl,
data: JSON.stringify(params),
dataType: 'json',
contentType: " application/json",
success: function (data) {
var myvalue= data.#odata.context; // ****???
var jsonObject = JSON.parse(data); //this line throws an error Unexpected token o in JSON at position 1
}
});
I think you can get data by this:
data["#odata.context"]
And about the JSON.parse throw exception, it caused by the data is not a JSON string.
Example
I need to pass both FormData and JSON object in an ajax call, but I get a 400 Bad Request error.
[artifact:mvn] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
[artifact:mvn] at [Source: java.io.PushbackInputStream#3c0d58f6; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
[artifact:mvn] at [Source: java.io.PushbackInputStream#3c0d58f6; line: 1, column: 3]
JS:
var formData = new FormData(form[0]);
//form JSON object
var jsonData = JSON.stringify(jcArray);
$.ajax({
type: "POST",
url: postDataUrl,
data: { formData:formData,jsonData:jsonData },
processData: false,
contentType: false,
async: false,
cache: false,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
success: function(data, textStatus, jqXHR) {
}
});
Controller:
#RequestMapping(value="/processSaveItem",method = RequestMethod.POST")
public #ResponseBody Map<String,String> processSaveItem(
#RequestBody XYZClass result[])
}
There is similar question, jquery sending form data and a json object in an ajax call and I'm trying the same way.
How can I send both FormData and JSON object in a single ajax request?
You can pass in this way
postdata={};
postdata.formData=formData;
postData.jsonData=jsonData
$.ajax({
type: "POST",
url: postDataUrl,
data: { postdata},
processData: false,
contentType: false,
async: false,
cache: false,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
success: function(data, textStatus, jqXHR) {
}
});
and controller side you can identify the data.
You can do the following to pass form data in Ajax call.
var formData = $('#client-form').serialize();
$.ajax({
url: 'www.xyz.com/index.php?' + formData,
type: 'POST',
data:{
},
success: function(data){},
error: function(data){},
})
I solved it by appending the JSON string with FormData object in ajax POST.
var jsonObjectData = JSON.stringify(jcArray);
formData.append("jsonObjectData",jsonObjectData);
and in controller you can access it the normal way as you do for other form data values.
request.getParameter("jsonObjectData");
and now you'll be having the Stringified JSON and you can parse the json to Java object
How to parse a JSON string to an array using Jackson.
My requirement is to grab the response from FBapi('/me',) and do a ajax post to a view to update the info to database. With the code below although I get a 200 on Ajax Post I am unable to retrieve the response using request.POST['name'],
request.POST['birthday'] etc .
If I simply give a:-
def GetFBData(request):
print(request)
I get: "WSGIRequest: POST '/login/facebook/" and an AJAX error message of Unexpected token at JSON position 1.
Below is my script :-
FB.api('/me', {
fields: 'birthday,cover,devices,email,first_name,gender,id,last_name,link,location,name,name_format,timezone,verified,website,locale'},
function(response) {
response = JSON.stringify(response);
$.ajax({
url: '/login/facebook/',
type: 'POST',
data:{fb:response , window:window.ui},
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
});
How to correctly retrieve this response data on the view?
I try using different instructions to use a JSON API from a Wordpress-System in a HTML-Teamplate. Unfortunately I do not succeed. Does anyone have any idea how I can read the section "Content" of http://www.earnyour21.de/api/get_page/?id=1588?
blog: function () {
$.ajax({
url: 'http://www.earnyour21.de/api/get_page/?id=1588',
type: 'GET',
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
}
If the data structure of the JSON will always be the same, you can simply access the object directly using the objects name in JS.
blog: function(){
$.ajax({
url: 'http://www.earnyour21.de/api/get_page/?id=1588',
type: 'GET',
dataType: 'json',
success: function(data){
$('#content_test').append(data['page']['content']);
},
error: function(data){
$('#content_test').append(data['page']['content']);
}
});
}
Basically you need to use jquery to grab the div with an id of content_test and then append your data from the json. http://api.jquery.com/append/ and http://www.json.com/ for further reference.