DekGenius.com
JAVASCRIPT
jquery ajax
$.ajax({
url: url,
dataType: "json",
type: "Post",
async: true,
data: { },
success: function (data) {
},
error: function (xhr, exception) {
var msg = "";
if (xhr.status === 0) {
msg = "Not connect.
Verify Network." + xhr.responseText;
} else if (xhr.status == 404) {
msg = "Requested page not found. [404]" + xhr.responseText;
} else if (xhr.status == 500) {
msg = "Internal Server Error [500]." + xhr.responseText;
} else if (exception === "parsererror") {
msg = "Requested JSON parse failed.";
} else if (exception === "timeout") {
msg = "Time out error." + xhr.responseText;
} else if (exception === "abort") {
msg = "Ajax request aborted.";
} else {
msg = "Error:" + xhr.status + " " + xhr.responseText;
}
}
});
jquery ajax
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
ajax jquery
$(document).ready(function(){
$('#btnadd').click(function(e){
e.preventDefault();
var nm =$('#nameid').val();
var em =$('#emailid').val();
var pas =$('#passid').val();
var mydata={name:nm,email:em,password:pas};
$.ajax({
url: 'insert.php',
method:'post',
data:mydata,
success:function(data){
console.log(nm);
}
});
});
});
jquery ajax
$.ajax('[URL]', {
method: 'POST',
dataType: 'json',
timeout: 5000
}).then(function (responseJSON) {
console.log(responseJSON);
}).catch(function(err){
console.log('Caught an error:' + err.statusText);
});
Jquery ajax
// this page loads the file "data.txt" when the button is clicked.
// This file is index.php.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn").click(function() {
$("#content").load("data.txt");
});
})
</script>
</head>
<body>
<div id="content"></div>
<br>
<button id="btn">Press Me</button>
</body>
</html>
jquery ajax
// jquery ajax
<script>
let categoryName = $('select[name="category"]')
let subcategory = $('select[name="subcategory"]')
categoryName.on('change',function(){
let myUrl = `{{ route('products.ajax', ':id') }}`
let id = $(this).val()
let newUrl = myUrl.replace(':id', id);
$.ajax({
url: newUrl,
dataType: 'json',
type: 'get',
success: function (response){
let options =[]
response.map(element => {
let option = `<option value="${element.id}">${element.name}</option>`;
options.push(option)
})
subcategory.html('')
subcategory.html(options)
},
error: function(data){
let option = `<option disable>${data.responseText}</option>`
subcategory.html('')
subcategory.html(option)
}
})
})
</script>
jquery ajax
//jquery ajax
// controller
public function product($id)
{
$category = SubCategory::where('category_id', $id)
->select('id', 'category_id', 'name')
->get();
if (count($category) > 0) {
return $category;
}else{
return 'no sub category';
}
}
// url
Route::get('/product/{id}', 'product')->name('ajax.product');
// product page
<script>
let categroy = $('select[name="slect_category"]')
let subCategroy = $('select[name="slect_sub_category"]')
categroy.on('change',function(){
let id = $(this).val()
let url = `{{ route('ajax.product', ':id') }}`
let newurl = url.replace(':id', id);
$.ajax({
url: newurl,
dataType: 'json',
type: "get",
success: function(response){
let options =[]
response.map(element =>{
console.log(element)
let option = `<option value="${element.id}">${element.name}</option>`;
options.push(option)
})
subCategroy.html('')
subCategroy.html(options)
},
})
})
</script>
© 2022 Copyright:
DekGenius.com