Laravel - Ajax error: xhr.send( options.hasContent && options.data || null ); - json

When I refresh the document, sometimes is working fine (I get the json object in console), but many times I get 500 (Internal Server Error) and from jQuery I see the error is: xhr.send( options.hasContent && options.data || null ); Please help me. Here is the code:
$(document).ready(function(){
$.ajax({
url: '/ajax/cis',
method: "GET",
success: function(data){
console.log(data);
}
});
});
'/ajax/cis' is a laravel controller wich only returns a json object:
return response()->json(['cis_names'=>'cis_names']);
I also tried with post method:
$.ajax({
url: '/ajax/cis',
method: "POST",
data:{_token: token},
//var token = Session::token()
success: function(data){
console.log(data);
}
});

Try this with using Blade and see if that works:
$.ajax({
url: '/ajax/cis',
method: "POST",
data:{_token: '{{csrf_token()}}'},
success: function(data){
console.log(data);
}
});

Related

Lexik Bundle Symfony 3

I use LexikAuthentication Bundle. When I try to make the UI using ajax for login_check, I get back the token in localStorage but when I try to login to my UI, I get 401 not authorization.
This is my login_check:
function authenticate(e) {
e.preventDefault();
$.ajax({
url: 'http://localhost:8000/login_check',
type: 'POST',
data: JSON.stringify({
'_username': $('#username').val(),
'_password': $('#password').val()
}),
dataType: 'json',
contentType: "application/json",
access_token: "",
crossDomain: true,
success: function (data) {
if (data != null && data != '') {
var token = "Bearer " + data.token;
console.log(token);
localStorage.setItem('token', data.token);
}
}
});
Anyone know how to fix this?
Okay i found the solution by my self.
The error was Here : localStorage.setItem('token', data.token);
It should be like this : localStorage.setItem("Bearer " ,data.token);

Json Error In Laravel Ajax

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?

Chrome extension transfer to manifest 2 - No ajax response

Hayush!
Been trying to transfer my Chrome extension to manifest 2. Everything is working except for one script which calls an ajax. I have a variable that contains a JSON content which needs to be transferred to the server.
function sendlist(list){
jsontext = JSON.stringify(list);
$.ajax({
url: amfurl + "user/listbookmarks/",
dataType: 'text json',
async: true,
type: 'POST',
processData: false,
data: {'folders': jsontext},
success: function(data){
$('#importing').css('display','none');
$('#importdone').css('display','block');
console.log(data);
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}
For some reason the ajax part is not executed at all. No error message was triggered, nor in the server error log.
All of the inline scripts and scripts in general were included in the popup.js. So it's probably not the issue.
Any thoughts?
Thanks!
The following code works perfectly on previous manifest
function importbookmarks(){
$('#formadd').css('display','none');
$('#importing').css('display','block');
_gaq.push(['_trackEvent', 'chromextension','importbookmarks', username]);
chrome.bookmarks.getTree(function(bookmarks) {
sendlist(bookmarks);
});
}
function sendlist(list){
jsontext = JSON.stringify(list);
$.ajax({
url: amfurl + "user/listbookmarks/",
dataType: 'text json',
async: true,
type: 'POST',
// processData: false,
data: {'folders': jsontext},
success: function(data){
$('#importing').css('display','none');
$('#importdone').css('display','block');
console.log(data);
}
});
}
The problem wasn't with the function. The button execution wasn't calling it correctly.
Here is the code I used to get the function to work in manifest 2
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("import-bookmarks").addEventListener("click", function () {
importbook();return;
});

JSON data not parsing in node.js displays undefined in console

JSON data not parsing in node.js displays undefined in console..
Here is HTML code:
var jsonObjects = [{id:1, name:"amit"}];
jQuery.ajax({
url: 'http://localhost:8081',
type: "POST",
data: {"id":"1", "name":"amit"},
dataType: "json",
success: function(result) {
alert("done")
}
});
Here is Nodejs code:
http.createServer(function (request, response)
{
response.writeHead(200, {"Content-Type":"text/plain"});
var urlObj = JSON.stringify(response.data, true);
console.log(urlObj)
response.end();
}).listen(8081);
Try using GET method Instead of Post. Try this
var jsonObjects = [{id:1, name:"amit"}];
$.ajax({
type: 'GET',
url: 'http://localhost:8081',
data: {
jsonData: JSON.stringify(jsonObjects)
},
dataType: 'json',
complete: function(validationResponse) {
}
});
The data will be held in the request, not the response as it has come from the client request.
If you are using express to create your http server you will also need to tell it to use the bodyParser middle wear.

Using jQuery to get jsonp data returns invalid label error

I have been using jquery ajax method to get the json data. I have used both jQuery.getJSON
and jquery.ajax methods but I keep getting the same error in my console. Invalid label meta I just want to display the some of the json data inside a div , is there something
wrong with the json file the java script works fine with other json files. I dont know how to parse this particular json file.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type = text/javascript>
$(document).ready(function(){
$.ajax({
url: 'http://api.yipit.com/v1/deals/?key=TMBYhd3hkzfCntMb&limit=20&division=boston',
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});
return;
});
</script>
I just tried to parse the data but I am not able to see the result.
In the console I get a error saying field.deals.url not defined ?
I am also able to see jQuery16209054896509423064_1311611178838({ on top of the
meta how can I parse this file ?
<script type = text/javascript>
$(document).ready(function(){
$.ajax({
url: 'http://api.yipit.com/v1/deals/?key=TMBYhd3hkzfCntMb&limit=20&division=boston&callback=?',
dataType: 'jsonp',
success: function(data){
$.each(data.response, function(i, field){
$('#display').append('<p>url : ' + field.deals.url+ '</p>');
if ( i == 3 ) return false;
});
}
});
return;
});
</script>
Delete jsonp: false, you have to use JSONP in this cross domain case.
This will work:
$(document).ready(function(){
$.ajax({
url: 'http://api.yipit.com/v1/deals/?key=TMBYhd3hkzfCntMb&limit=20&division=boston',
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});
});
Working example: jsFiddle
EDIT:
deals is your iterator not response
success: function(data){
$.each(data.response.deals, function(i, field){
$('#display').append('<p>url : ' + field.url+ '</p>');
if ( i == 3 ) return false;
});