Skip to content

Instantly share code, notes, and snippets.

@moradi-morteza
Last active June 28, 2020 06:07
Show Gist options
  • Save moradi-morteza/79581705a62a6d5b4cf489db770340d1 to your computer and use it in GitHub Desktop.
Save moradi-morteza/79581705a62a6d5b4cf489db770340d1 to your computer and use it in GitHub Desktop.
ajax.jquery
https://api.jquery.com/jquery.ajax/
$.ajax({name:value, name:value, ... })
complete(xhr,status) A function to run when the request is finished (after success and error functions)
success(result,status,xhr) A function to be run when the request succeeds
error(xhr,status,error) A function to run if the request fails.
data Specifies data to be sent to the server
dataType The data type expected of the server response.
timeout The local timeout (in milliseconds) for the request
type Specifies the type of request. (GET or POST)
url Specifies the URL to send the request to. Default is the current page
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
$.ajax({
statusCode: {
404: function() {
alert( "page not found" );
}
}
});
----------------------------------------------------
return response()->json(['message'=>"success",'data'=>'this is data'],200);
return response()->json(['error'=>"this is error"],404);
$.ajax({
url: url,
method: 'GET',
dataType: 'JSON',
success: function (result) {
var data = result.data;
alert(result.message);
}, error: function (result) {
alert(result.status);
var response = $.parseJSON(result.responseText);
alert(response.error);
}
});
$.ajax({
url: url,
method: 'POST',
dataType: 'JSON',
data: {
_token: csrf,
name: 'ali',
},
success: function (result) {
var data = result.data;
alert(result.message);
}, error: function (result) {
alert(result.status);
var response = $.parseJSON(result.responseText);
alert(response.error);
}
});
-------------------------------------------------------------------
<meta name="csrf-token" content="{{ csrf_token() }}">
<meta name="site_url" content="{{config('app.url')}}">
var site_url = $('meta[name="site_url"]').attr('content');
var csrf = $('meta[name="csrf-token"]').attr('content');
$.ajaxSetup({headers: {'X-CSRF-TOKEN': csrf}});
$.ajax({
url: site_url + "/lang/"+id,
type: 'DELETE',
dataType: "json",
success: function (result) {
)
}, error: function (request, status, error) {
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment