i want to know how to access object response properties from a ajax call, i'm working with laravel 5.2 and Ajax, and i have this one:
$.get('/provider/'+provider_id+'', function(response){
console.log(response);
});
Yeah i tried with response = JSON.parse(response);but i got this error Uncaught SyntaxError: Unexpected token o in JSON at position 1(…) what's wrong?? or how i should to do? thanks!
As you can see from the console output, response is already an Object. No need to parse it again.
$.get('/provider/' + provider_id, function(response){
var data = response.response;
console.log("Email: " + data.email_p);
});
Related
I am trying to access the 'items' array in a response I get using the 'get' function in the 'request' node module. As you can see below I get the entire response and can log it to the console, but when I try to access the 'items' property within that response it says it's undefined:
console.log("r.body: " + r.body);
response:r.body: {"limit":-1,"totalCount":1,"items":[{"id":1958,"status":"PULLED"...
console.log("r.body: " + r.body.items);j
response: undefined
To avoid parsing the response each time (JSON.parse(response.body)) you can use the defaults method to tell the request module to always parse the JSON. like so:
var req = request.defaults({
json: true
})
This is useful if you are repeatedly communicating with an API.
var url="http://fsa.citop.in/lnct/service/signProcess.aspx";
var data={txtLogId: "abc#xyz.com",txtLogPass: "xyz",hdnReqType2: "sign87162"};
var success=function(data, textStatus, jqXHR) {
console.log(data);
};
var fail=function(jqXHR, textStatus, errorThrown) {
console.log("Error:" + errorThrown );
}
$.ajax({
type: "POST",
url: url,
data:data,
success:success,
error:fail,
});
This POST request gives me the error, SyntaxError: Unexpected token < in JSON at position 4, in the console of the page 'http://fsa.citop.in/lnct/' in chrome.
But if I use fsa.citop.in/lnct/service/signProcess.aspx (i.e. no http://), it gives me no error, but nothing comes back in data. On success of POST request, a JSON object is expected. Please somebody explain what is happening here and how it could be resolved.
For those who encounter this problem in AWS Lambda code editor it is most likely your session has timed out.
Try reloading the page and signing in again.
It should resolve this.
It's most likely because the response is HTML and it's trying to parse it as something else. The < at position 4 is the first < of <!DOCTYPE html....
You should try to specify dataType in your ajax call (see http://api.jquery.com/jquery.ajax/) and also make signProcess.aspx to return something more useful (currently the response content type is application/json but it prints HTML).
I am trying to send a Django HttpResponse encoded as JSON to Javascript.
Python:
response_data = {}
response_data['status'] = 'incomplete'
return HttpResponse(json.dumps(response_data), content_type="application/json")
Jquery:
function auth_request(){
$.ajax({
url: AUTH_ENDPOINT + "myid0001",
context: document.body,
success: function(response){
console.log(response);
console.log(response.status);
if(response.status == "incomplete"){
//do something here
}
}
}
});
}
The console prints {"status": "incomplete"} for the first console log and undefined for the console.log function accessing the status element.
I tried using JSON.parse(response) but I get the error
Uncaught SyntaxError: Unexpected token a in the jquery.js file which I believe is indicating that the object is already a JSON object. However, if I check the type of the object, it displays string. How can I access the elements of the response JSON object?
You need to parse the Json back into a JS object when it's received. Otherwise, it's just text.
jQuery will do that for you if you specify dataType: "json" in the Ajax call.
I found the solution. It turns out that my Django setup was returning some additional string items so when I tried to set dataType: "json" (as #Daniel Roseman suggested) or in the jQuery function or use JSON.parse(response) I received an error. I was able to remove the extra string and it worked properly.
I'm trying to get data from a JSON via Yahoo Query Language (YQL) using jQuery.
Link to JSON
index.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.unisul.br%2Fwps%2Fportal%2Fhome%2Fconheca-a-unisul%2Fa-universidade%2Fcampus-unisul-virtual%2Fpolos-presenciais'%20and%20xpath%3D'%2F%2F*%5B%40id%3D%22lista-polos%22%5D'&format=json&diagnostics=true&callback=",
dataType: 'jsonp',
success: function (response) {
var polos = response.results[0];
var getPolosHTML = '';
console.log(polos);
}
});
</script>
Error in console:
Uncaught SyntaxError: Unexpected token :
Any solution?
First, you have to setup the url so that jquery will add in the jsonp param by adding a ? to callback=
...&callback=?
then, you have to modify the success callback to properly reference the data.
var polos = response.query.results;
console.log(polos); // object with a div property
http://jsfiddle.net/jZ4n8/1/
There is no error in code. Output of the api is invalid. You can cross check json result(http://jsonlint.com/).
OK I'm using PHP and the Facebook JS API to post stories on users' pages with jQuery's $.ajax() function and it's working fine everywhere except in Chrome.
Chrome is returning the error "SyntaxError: Unexpected Token : ".
I have it alerting the XHR response on error and it is as follows:
{
"id" : "30800681_37922830145443"
}
which is valid JSON. It can't be anything I'm doing wrong with the JSON result because it throws the error before any parsing can be done (i.e., it's not making it into the 'success' function).
The code that's behind this is as follows:
if ($('#post-facebook').is(":checked")) {
// Do the FB post
$.ajax({
type: "POST",
url: "https://graph.facebook.com/<?= $this->session->userdata('fb_id') ?>/feed",
data: "message=" + $("#upload-caption").val() + "&access_token=<?= $this->session->userdata('fbtoken'); ?>&app_id=<?= $this->config->item('appId') ?>&link=" + post_url,
success: function(msg) {
// Save the FB post ID to the DB
var result = $.parseJSON(msg);
var result_array = result.id.split("_");
// Do more stuff here, but it's not even getting into this success function
},
error: function(xhr,ajaxOptions,thrownError) {
// This is what's executing because the thrown error is getting alerted
alert(thrownError);
}
});
}
When I add dataType: "json" in the Ajax parameters, it still goes through the Error function but the thrownError parameter is empty.
I am pulling my hair out...any suggestions? Thanks in advance,
Using jQuery with post is not possible as POST require CORS support, and that is not readily available.
Use FB.api instead, which handles all of this for you, in a much better way.