I want to perform an Ajax post operation via Detail controller with Ajax. But I am encountering this error:
Unable to load the requested language file:
language/positive-flag/general_lang.php
Ajax:
$('#positive').on('click', function() {
$.ajax({
type: 'POST',
url: '/positive-flag',
data: dataJson,
success: function(response) {
console.log(response)
},
error: function(error) {
console.log(error)
}
});
});
Route File:
$route['positive-flag']['post'] = 'detail/positiveFlag';
$route['negative-flag']['post'] = 'detail/negativeFlag';
$route['unknown-flag']['post'] = 'detail/unknownFlag';
I do not want the language feature to be attached to these functions.
I hope I could explain what I mean.
I fixed.
solution is use fetch_method() function.
Related
I really don't understand this:
jQuery('.myCheckbox').click(function() {
if($(this).is(':checked'))
{
$.ajax ({
type: "POST",
url: "comparecustom",
data: {
'product_id': prdidtable,
'term_id': checkboxData,
'sort_list_id': sort_list_id
},
success: function(data) {
console.log(data);
},
complete : function(){ // this will be called even if our request fail, success etc
}
});
$('.mybestall_'+checkboxData).prop('checked',false);
}
else if($(this).not(':checked'))
{
$.ajax ({
type: "POST",
url: 'comparecustom',
data: {
'product_id': prdidtable,
'term_id': checkboxData,
'delete_data': delete_data
},
success: function(data) {
console.log(data);
},
complete : function(){ // this will be called even if our request fail, success etc
}
});
}
});
My Route:
Route::post('comparecustom', array('before' => 'csrf', 'uses' => 'compares#comparecustom'));
My Contrlller:
if(Request::AJAX())
{
}
In works fine in Localhost, but when I transfer to live server returns POST/PUT 500/404 error respectively. I have tried many different ways still cant find a solution.
it works perfectly in localhost, why doesn't it work in live server?
Please help? csrf_token?
Could it be csrf_token which I need to pass along with Ajax call:
I tried that too no luck
ok! I have solved it by having a "/" in my route. "/" was added in the beginning:
Route::post('/products/(:any)/comparecustomdata', array('uses' => 'compares#comparecustomdata'));
all other route do not have "/". that was confused me.
so problem solved.
thank you everyone for your help
Am using laravel 4.2 and trying to update data using ajax but in ajax redirect url not working for me. Because am using URL:action like,
$.ajax({
type: 'POST',
dataType: 'json',
url : "store",
data: { title: postTitle['post-title']['value'], body: postContent['post-body']['value'] },
success: function(data) {
if(data.success === false)
{
$('.error').append(data.message);
$('.error').show();
} else {
$('.success').append(data.message);
$('.success').show();
setTimeout(function() {
window.location.href = "{{ URL::action('PostsController#index') }}";
}, 2000);
}
},
error: function(xhr, textStatus, thrownError) {
alert('Something went wrong. Please Try again later...');
}
});
i dont know why its not working. please help me.
You need to add a route in your routes file:
Route::post('post', 'PostsController#index');
But if you enabled CSRF, then you also need to post the CSRF code. You can do this through add this in your post "data".
...
url : "{{ url('store') }}",
Data: data: { title: postTitle['post-title']['value'], body: postContent['post-body']['value'] }, _token: {{ csrf_token() }},
...
I hoped this works for you!
What are you doing here is a really terrible practice. You should never use dynamically created JS code in a real app, if you have a choice.
First of all, you're tight coupling JS and PHP code (kind of anti-MVC). Request time increases. It's harder to maintain the app. You are not able to use prepared (minified) JS etc.
Why generating JS with PHP is a bad practice
Here, you should create URL manually:
window.location.href = "/post/something";
Just create route and use it without URL::
Route::post('post/something', 'PostsController#postSomething');
Add a route in your route file:
Route::get('post','PostsController#index');
change js to:
setTimeout(function() {
window.location = "<?php echo URL::action('PostsController#index') ?>";
}, 2000);
or:
setTimeout(function() {
window.location = "<?php echo action('PostsController#index') ?>";
}, 2000);
I am trying to upload a file using jQuery's $.ajax function but didn't get any output.
Somebody please help me to solve this.
I don't know if this script is correct.
My script is:
$.ajax({
url:'newsup.php',
data: "",
type: 'POST',
contentType:'multipart/form-data',
dataType: 'json',
catche: 'false',
success:function(data6)
{
$("#disp").removeClass().addClass((data6.error=== false)? 'success':'error').html(data6.msg).fadeIn('fast');
//dele();
if($("#disp").hasClass('success'))
{
alert("success");
setTimeout("$('#disp').fadeOut('slow')",3000);
}
},
error:function(XMLHttpRequest,textStatus,errorThrown)
{
$("#disp").removeClass().addClass('error').html("There was an <strong>"+errorThrown+"</strong> error due to <strong>"+textStatus+" condition").fadeIn('fast');
}
});
Also I need help getting data from file uploading field using jQuery.
Please use plugin for this.In my opinion this plugin is better solution for this.You don't need to remember all options etc.Just replace your 'ajax' to 'ajaxForm'.
Please read examples ,below
http://jquery.malsup.com/form/#ajaxForm
This is how I've done it. Use the FormData object.
Note: The odd syntax of the for statement is just setting "f" to the array[i] instance.
$("#submit").click(function () {
var formData = new FormData();
for (var i = 0, f; f = fileArray[i]; i++) {
formData.append("opmlFile", f);
}
$.ajax({
url: "/Documents/SaveFiles/" + #Model,
type: "POST",
data: formData,
cache: false,
contentType: false,
processData: false
})
.error(function (xhr, status, error) {
$.notify(error, true);
})
.success(function (data, status, xhr) {
$.notify("Success");
});
});
Unfortunately I don't recall which article I got this from, but it was someone else on Stack Overflow.
AJAX doesnt support file uploading. There are plugins like ajaxfileupload which basically creates a hidden form and uploads your file dynamically.
take a look here and read Oli's answer
I'm using this and it's working fine:
$('#btnUploadFile').on('click', function () {
var data = new FormData();
var files = $("#fileUpload").get(0).files;
// Add the uploaded file content to the form data collection
if (files.length > 0) {
data.append("upload", files[0]);
}
// Make Ajax request with the contentType = false, and procesDate = false
var ajaxRequest = $.ajax({
type: "POST",
url: "/api/documents",
contentType: false,
processData: false,
data: data,
error: function (xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
console.log(data);
}
});
ajaxRequest.done(function (xhr, textStatus) {
$("#response").attr('class', "alert alert-success");
$("#response").html("File uploaded successfully");
});
});
You can use either of the two plugins Jquery File Upload Plugins 1 or Jquery File Upload Plugins 2 and there's no errors on this script.
Hope it helps
Thanks,
Rashid
Ajax supports File upload using FormData Object, Also supports in all major browser except IE8/9
See below
https://developer.mozilla.org/en-US/docs/Web/API/FormData
Another option would be to base64 encode the file contents and send it as a string, decoding it at the back-end.
Simply use submit event on form to send the files and prevent default form action
$('#form').submit(function(e) { return false; });
and get the file on the server side via
$_FILES['inputName'];
Everything was going great in my previous help request thread. I was on the correct track to get around a CSRF, but needed to be pointed in the right direction. I received great help and even an alternate script used to log into Google's Android Market. Both my script and the one I altered to match my form is get hung up at the same point. Apparently cURL cannot process JS, is there any way to work around the form being submitted with submitForm() without changing the form?
Here is the code for the SubmitForm function
function submitForm(formObj, formMode) {
if (!formObj)
return false;
if (formObj.tagName != "FORM") {
if (!formObj.form)
return false;
formObj = formObj.form;
}
if (formObj.mode)
formObj.mode.value = formMode;
formObj.submit();
}
Here is the code for the submit button -
<a class="VertMenuItems" href="javascript: document.authform.submit();">Submit</a>
Here is a link to my last question in case more background information is needed.
PHP service...
<?php
// PHP service file
// Get all data coming in via GET or POST
$vars = $_GET + $_POST;
// Do something with the data coming in
?>
Javascript elsewhere...
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function sendData(data)
{
var response;
$.ajax({
url: 'phpservice.php',
data: data,
type: 'POST',
dataType: 'json',
async: false,
success: function(response_from_service)
{
response = response_from_service;
},
error: function()
{
}
});
return response;
};
function getData(data)
{
var response;
$.ajax({
url: 'phpservice.php',
data: data,
type: 'GET',
dataType: 'json',
async: false,
success: function(response_from_service)
{
response = response_from_service;
},
error: function()
{
}
});
return response;
};
});
</script>
I'm building a chatroom that sends messages via AJAX. Whenever I hit enter, with the data: parameter, it returns an error, but if I remove data:, it doesn't do anything. This is very puzzling, and I'm wondering what I'm doing wrong. Here is what I have:
$("#form").bind("keypress", function(e) {
if (e.keyCode == 13) {
$.ajax({
type: 'POST',
url: "send-message.php",
data: "message="+$("#message").val()+"&user="+$("#user").val()+"&room="+$("#room").val(),
success: $("#message").val(""),
error: $("#message").val("FAIL"),
});
return false;
}
});
I use PHP in my AJAX call, so I don't know if that is causing the problem?
Try this:
...
$.ajax({
type: 'POST',
url: "send-message.php",
data: {message: $("#message").val(), user: $("#user").val(), room: $("#room").val()},
success: function() { $("#message").val(""); },
error: function() { $("#message").val("FAIL"); }
});
...
In the above code:
a) data sent as JSON - this will make sure that any url encoding and escaping will be correctly performed by jQuery as needed
b) success and error options must be callback functions
I would do this just to check if it grabs all the data correct:
var data = {
message: $('#message').val(),
user: $('#user').val
};
console.log(data);
You need to change these lines:
success: $("#message").val(""),
error: $("#message").val("FAIL"),
to
success: function () { $("#message").val("") },
error: function () { $("#message").val("FAIL") // removed trailing comma
I wrapped your handlers in a function and removed the trailing comma.
You can pass an object into data, and jQuery takes care of transforming it into the correct form for the type of request you issue, in this case a POST:
$("#form").bind("keypress", function(e) {
if (e.keyCode == 13) {
$.ajax({
type: 'POST',
url: "send-message.php",
data: {
message: $("#message").val(),
user: $("#user").val(),
room: $("#room").val()
},
success: function () {
$("#message").val("");
},
error: function () {
$("#message").val("FAIL");
},
});
return false;
}
});
Also, error and success are callbacks, so you need to provide a function, not a string if you want it called. Fixed it for you.
If you want to pass your data in POST you should use the javascript key:value format (or JSON format).
Input the following in
data: {"message": $("#message").val(),"var2":variable}
and a hint
...
data: $("#form").serialize();
...