Can't pass data from attributes using ajax laravel 5.1 - ajax

var formData = new FormData();
formData.append('name', name.val());
formData.append('email', email.val());
formData.append('contact_number', contact_number.val());
formData.append('barangay', barangay.val());
formData.append('landmark', landmark.val());
formData.append('password', password.val());
formData.append('confirm_password', confirm_password.val());
//Problem start here it came from attributes but I can alert the data and it was fine.
formData.append('image_pin', img_top);
formData.append('img_left', img_left);
formData.append('img_z_index', img_z_index);
formData.append('input_value', input_value);
formData.append('input_top', input_top);
formData.append('input_left', input_left);
$.ajax({
url: 'register',
headers: {'X-CSRF-TOKEN': token.val() },
method: 'post',
processData: false,
contentType: false,
cache: false,
dataType: 'json',
data: formData,
success: function(data){
console.log(data);
},
error: function(){
}
});
//The input works fine but the attributes data is undefined
$request->input('img_top');
$request->input('img_left');

Related

laravel and sweetalert and rederect to

i use laravel 5.8 and i have a a problem if i insert or update a page than i go not back to the page list.
postcontroller :
i use : return response()->json('Post Created');
and i use a ajax file whit this code :
$.ajax({
type: 'POST',
url: this.action,
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
success: function(response){
Sweet('success',response)
success(response)
},
error: function(xhr, status, error)
{
$('.errorarea').show();
$.each(xhr.responseJSON.errors, function (key, item)
{
Sweet('error',item)
$("#errors").html("<li class='text-danger'>"+item+"</li>")
});
errosresponse(xhr, status, error);
}
})
});
i get a nice message if it is insert or updated but it not go back to my list, hope you understand what i mean.
if you want to redirect after success do something like this:
$.ajax({
type: 'POST',
url: this.action,
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
success: function(response){
Sweet('success',response)
success(response)
window.location.href = "url you want to redirect to";
},
error: function(xhr, status, error)
{
$('.errorarea').show();
$.each(xhr.responseJSON.errors, function (key, item)
{
Sweet('error',item)
$("#errors").html("<li class='text-danger'>"+item+"</li>")
});
errosresponse(xhr, status, error);
}
})
});

Youtrack - Apply Command to an Issue requires log in

To apply a command to an issue i'm using this code:
$.ajax({
async: false,
type: 'post',
url: "https://golaservices.myjetbrains.com/youtrack/rest/issue/"+e.target.id+"/execute"+"?command="+target.val(),
cache: "true",
dataType: "html"
});
but that brings up this error
You have no access to this resource. Try to log in.
that's why i tried putting that first bit of code like this:
$.ajax({
async: false,
type: 'post',
url: "https://golaservices.myjetbrains.com/youtrack/rest/user/login?login=xxx&password=123",
cache: "true",
dataType: "html",
success: function(output, status, xhr) {
alert(xhr.getResponseHeader('Set-Cookie'));
}
}).done(function (data) {
console.log("in done");
$.ajax({
async: false,
type: 'post',
url: "https://golaservices.myjetbrains.com/youtrack/rest/issue/"+e.target.id+"/execute"+"?command="+target.val(),
cache: "true",
dataType: "html"
});
console.log("after done");
});
but i still get the same error, any idea how to do this ?
/rest/user/login endpoint has been deprecated for a while already and is not recommended for further use. Try permanent tokens instead, they don't require any preflight login requests to be executed.
$.ajax({
type: "post",
url: "https://golaservices.myjetbrains.com/youtrack/rest/issue/" + e.target.id + "/execute" + "?command=" + target.val(),
dataType: "json"
beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Bearer " + permanentToken); },
});

How to Display Json Data when ajax request is success

Here I am using Ajax+Mvc when I call my data from database it comes in Json format but how can I display that data in div?
Html
<div id="Div1">
</div>
<b>Get data</b><input type="button" id="BtnGetData" value="GetData" />
When user click on BtnGetData the data should be display in #Div1 Here my AJax working fine.But please suggest me where to write $('#Div1').load();
$('#BtnGetData').click(function () {
alert('ok data')
$.ajax({
url: '/Home/Getdata',
method: "GET",
type: "JSON",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(),
})
})
MvcController
public JsonResult Getdata()
{
var x = Objrepo.GetEmplo();
return new JsonResult { Data = x, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
You can write to the div on the success function of your $.ajax call.
eg:
$('#BtnGetData').click(function () {
$.ajax({
url: '/Home/Getdata',
method: "GET",
type: "JSON",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(),
success: function (data) {
console.log(data);
}
})
});
You need to add a success callback to your ajax:
$('#BtnGetData').click(function () {
alert('ok data')
$.ajax({
url: '/Home/Getdata',
method: "GET",
type: "JSON",
contentType: 'application/json; charset=utf-8',
success: function(data){
$("#Div1").append(data);
}
})
})
There is no need to add the data property to the ajax if your are not sending anything.
May be, you can add this function in your script.
$('#BtnGetData').click(function () {
alert('ok data')
$.ajax({
url: '/Home/Getdata',
method: "GET",
type: "JSON",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(),
writeResponse(data)
})
});
writeResponse(data){
$("Div1").html(data);
};

Send FormData and String Data Together Through JQuery AJAX

I have the below that uploads an image using JQuery's AJAX function
var id = <?php echo $id; ?>;
var img_data = new FormData($("form")[0]);
$.ajax({
url: 'add.php',
data: img_data,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
I'd like to include a string with the FormData sent. I tried the following, but no luck
data: img_data {id: id},
What's the correct syntax here?
Use append
var img_data = new FormData($("form")[0]);
img_data.append('id', id);
You could create a JSON data object and pass that as application/json and process the data within add.php:
var data = {
id : <?php echo !empty($id) ? $id : "''",
img_data : new FormData($("form")[0])
};
$.ajax({
url: 'add.php',
data: data,
contentType: "application/json",
type: 'POST',
success: function(data){
alert(data);
}
});
Although unconventional, you could also append the data as a query string to the URL with the POST. You'd also need to edit add.php to get this parameter.
$.ajax({
url: 'add.php?id=' + id,
data: img_data,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});

Ajax post parameters ASP.NET MVC 3

Hello guys i have the next ajax call for login. I serialize the form and send the data to server and return redirect url link. My problem is that my url after post is like
http://localhost:50802/?username=&password= and not http://localhost:50802/Home
$.ajax({
type: "POST",
url: "Login/Login",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: loginJson,
cache: true,
async: false,
complete: function (result) {
alert(result.link);
window.location.replace = "/Home/Index";
},
error: function () {
$("#username").val("");
$("#password").val("");
alert("Wrong Username or Password!");
}
}); //end ajax call
It looks like you wrote this $.ajax call in the .click event of a submit button or in the .submit event of a form without canceling the default action by returning false from the callback or by calling preventDefault on the argument. Here's how your code should look like:
$('#id_of_your_form').submit(function(e) {
e.preventdefault(); // <-- That's what I am talking about and what you forgot
$.ajax({
type: "POST",
url: "Login/Login",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: loginJson,
cache: true,
async: false,
complete: function (result) {
window.location.replace = "/Home/Index";
},
error: function () {
$("#username").val("");
$("#password").val("");
alert("Wrong Username or Password!");
}
}); //end ajax call
});
Also async: false,????? You know what this does, do you? That's not AJAX. That's a blocking synchronous call to your webserver during which the client browser would be frozen like during the Ice Age 2 ruining all user experience.
Try returning false at the end of your submit function
$('#id_of_your_form').submit(function(e) {
$.ajax({
type: "POST",
url: "Login/Login",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: loginJson,
cache: true,
async: false,
complete: function (result) {
window.location = "/Home/Index";
},
error: function () {
$("#username").val("");
$("#password").val("");
alert("Wrong Username or Password!");
}
}); //end ajax call
return false; });
Another option would of course be to return the correct redirectlink from the controller instead of overriding it in the java script.

Resources