500 (Internal Server Error) in ajax data posting laravel 5.4 - php

hi i am trying to post some values on drop down change and fetch data form database on the base of that value and return back the ajax response .
like i want to get all branches of that specific company when i change company by drop-down, branches of that specif company displayed in drop down in a specific area
but i am facing 500 (Internal Server Error). Always displays i am an error in console. My jQuery is
$(".select-company").dropdown({
onChange: function (val) {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
var id=val;
var urll = "promoplan/getcity";
$.ajax
({
type: "POST",
url: urll,
data: { company_id: id, _token: CSRF_TOKEN },
cache: false,
success: function(html)
{
console.log(html);
//$("#regions").html(html);
},
error: function()
{
console.log('i am an error');
}
});
}
});
My Route is
Route::post('promoplan/getcity','PromotionPlanController#getcity');

In your controller you have to incude following
use Response;

Can you try var urll = "promoplan/getcity"; to urll = "/promoplan/getcity";

Related

How to Select Data from database using Ajax and Plugin Select2

Please help me to solve my problem.
$("#kategori_laporan").change(function() {
$("#loaderIcon").show();
$("#imgLoader").show();
var id = $(this).val();
$.ajax({
type: "POST",
dataType: "html",
url: "_views/getAjaxSubKategori.php",
data: "id="+id,
success: function(data) {
$(".select-skategori").select2({
ajax: {
url: "_views/getAjaxSubKategori.php",
dataType: 'json',
data: function(term, page) {
search = term.toUpperCase();
},
results: function(datas, page) {
return {
results: data
};
}
},
formatResult: function(option) {
return "<div>" +option._sub_kategori_laporan+ "</div>";
},
formatSelection: function(option) {
return option._sub_kategori_laporan;
}
});
}
});
});
Data not showing to subcategory select. Thanks before.!
Screenshot >> http://i.imgur.com/FlCeGpo.jpg
Why are you firing an ajax request before you initialize the select2 box?
I ask this, because your select2 box is requesting the search results via ajax as well. But maybe there is a reason. Please explain.
Then I can see in your screenshot, that your data returned from the json ajax request has not defined values id and text. If you are using other values ('_sub_kategori_id' and '_sub_kategori_laporan') in your json result, you have to tell the select2 box, what is your id and text field. I think you can do this with the processResults method as described here:
How to load JSON data to use it with select2 plugin

Laravel 5.1 - Ajax call Error 500 (for only one function)

I have a weird problem which I can't find a solution for.
Ajax code:
$("#details-comment-btn").click(function(e){
e.preventDefault();
var functionType = "comment";
var articleid=$('input[name=articleId]').val();
var owner_id=$('input[name=owner_id]').val();
var first_name=$('input[name=owner_first_name]').val();
var last_name=$('input[name=owner_last_name]').val();
var token=$('input[name=_token]').val();
var commentBody = $('textarea[name=commentBody]').val();
console.log(articleid);
$.ajaxSetup({headers : {'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')}});
$.ajax({
type: "POST",
url:articleid,
data:
{
'user_id': owner_id,
'commentBody': commentBody,
'functionType': functionType,
'article_id': articleid,
'_token': token
},
success: function(){
console.log(articleid);
$('#newcomment').append('<div class="comment" id="newCommentDiv">');
$('#newCommentDiv')
.append('' + first_name + " " + last_name + '')
.append('<span class="date">Just Now</span>')
.append('<p class="body">'+ commentBody + '</p>');
$('#newcomment').fadeIn();
}
});
});
However, I always get the same error
POST http://project.dev/articles/1 500 (Internal Server Error)
Here's the route I have for the post
Route::post('articles/{id}', 'DeleteController#deleteWork');
The link it show is fine, so I'm not sure why it's giving me the error.
The weird part is that other ajax calls who need to same URL work without any problems.
For example, here's the code from another AJAX call which does work and uses the same "url".
$('.articleFavo').click(function(e){
e.preventDefault();
var userid= $('input[name=userID]' ).val();
var functionType = 'favorite';
var articleid=$('input[name=articleId]').val();
var token=$('input[name=_token]').val();
$.ajaxSetup({headers : {'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')}});
$.ajax({
url:articleid,
type: "post",
data:
{
'functionType': functionType,
'user_id': userid,
'article_id': articleid,
'_token': token
},
success: function(){
console.log(articleid);
$('#favoriteBtn'+ articleid).css({display: 'none'})
$('#unfavoriteBtn'+ articleid).fadeIn();
}
});
});
On line 378 in your controller, you have the following line:
$article_id = input::get('articleId');
However, you're sending up "article_id" in your AJAX call. It'll probably be better to update this line in the controller (to be consistent with the rest of your code)
$article_id = input::get('article_id');

Yii ajax call and session handling

I stucked in Yii, with an Ajax call "bug". The problem is, that i have to search by checkboxes, wich are stored in SESSION after hitting the search button.
After this, the keywords inserts into SESSION, and i want to print them in a tag-cloud. (with a cross in the corner -> delete this tag).
The removal of these tags is handled by an ajax call:
$(".tags a").click(function(e) {
e.preventDefault();
var class1 = $(this).attr("class");
var id = $(this).attr("id");
$("#checkbox__"+id).removeAttr("checked");
jQuery.ajax({
url: "recipe/removeFromSession",
type: "POST",
data: "id=" + class1+"&rcp="+id,
dataType: "json",
"success": function(data){
location.reload();
},
"error": function(xhr, status, error) {
var err = xhr.responseText;
alert(xhr.status);
},
"cache" : false,
});
});
It calls the removeFromSession action:
public function actionRemoveFromSession() {
$remove = intval($_POST['id']);
unset($_SESSION['searchItems']['category'][$remove]);
unset($_SESSION['recipeSearch']['category'][$_POST['rcp']]);
echo json_encode($_SESSION);
}
The problem is that it doesn't unsets the value. It's still in the session. Any idea? (sorry for grammar mistakes)
Thanks
You can try this:
Set your session:
Yii::app()->session['searchItems']['category'][$keyword] = "Any string";
To unset session:
unset(Yii::app()->session['searchItems']['category'][$remove]);

jQuery ajax request awkward issue

So I have this ajax request. When the user clicks an edit link, I fetch the ID of the entry and refresh the page with the data of that entry loaded into a form.
Here's my problem: This only works with the alert showing before the ajax call. When I leave out the alert, I get an ajax error (though the id is being posted) and the PHP page just reloads. Moreover, it only works when I put the newDoc stuff as a success callback. The exact same lines as a complete callback and the page reloads. Moreover, this occurs in Firefox only.
jQuery('a.edit').on('mousedown', function (e) {
e.preventDefault();
var id = jQuery(this).attr('data-title');
alert('test');
jQuery.ajax({
url: document.location,
data: {
id: id
},
success: function (data) {
var newDoc = document.open("text/html", "replace");
newDoc.write(data);
newDoc.close();
},
error: function () {
alert('error');
}
});
});
What can I do?
EDIT: This must be a timing issue. I just noticed that when I click and hold the edit link for a second or so, everything works fine. When I do a short click, it doesn't. So I tried wrapping the ajax in setTimeout(), but that didn't help. Any other ideas?
Try to use location.href in place of document.location,
jQuery.ajax({
url: location.href,
data: {
id: id
},
success: function (data) {
var newDoc = document.open("text/html", "replace");
newDoc.write(data);
newDoc.close();
},
error: function () {
alert('error');
}
});
location is a structured object, with properties corresponding to the parts of the URL. location.href is the whole URL in a single string.
Got it!
The problem is the way Firefox handles the mousedown event. It seems to abort the ajax call as soon as you relase the mouse button. I changed the event to click and everything is fine now.
jQuery('a.edit').on('click', function () {
var id = jQuery(this).attr('data-title');
jQuery.ajax({
url: document.location,
data: {
id: id
},
success: function (data) {
var newDoc = document.open("text/html", "replace");
newDoc.write(data);
newDoc.close();
}
});
});

Submit to multiple php scripts

I have a javascript function which I'm using to change the action field of a form and then submit it. Here's the function
function printmulti(){
form=document.forms['form2'];
form.action="http://localhost/output_sample1.php/";
form.target = "_blank"; // Open in a new window
form.submit();
form.action="http://localhost/output_sample2.php/";
form.target = "_blank";
form.submit();
return true; }
But somehow only output_sample2.php is being shown. Why isn't the first part of the code being executed?
you cant submit to multiple forms like that, you need to use something like ajax and make the requests that way. Currently you are starting the submit for the first and then starting the second right after so the second one stops the first one from submitting.
Ajax Tutorial
Use ajax like this:
$.ajax({
type: 'POST',
url: 'http://localhost/output_sample1.php/',
data: 'var1='+var1+'&var2=var2', //your variables sent as post at output_sample1.php
success: function( data ) {
//do success stuff
},
error: function(xhr, status, error) {
alert(status); //if any error
},
dataType: 'text'
});
$.ajax({
type: 'POST',
url: 'http://localhost/output_sample2.php/',
data: 'var1='+var1+'&var2=var2', //your variables sent as post at output_sample2.php
success: function( data ) {
//do success stuff
},
error: function(xhr, status, error) {
alert(status); //if any error
},
dataType: 'text'
});
Hope will give you some idea to start your work. For more info visit this link ajax example

Categories