Get request returning blank response - ajax

I am trying to call a url using ajax. it works well using browser but while calling through ajax it return blank.
Anyone please help. Thanks in advance below is code i am using..
<script>
$( document ).ready(function() {
$.ajax({
type: "get",
dataType:"json",
crossDomain :true,
url: "http://cloud1.autoweek.com/widgets/test-read-more/get-article-json.php?nodeid=54321&start=123123123",
success: function(response){
alert("success");
},
complete:function(){
alert("complete");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
console.log(textStatus);
}
});
});
</script>

Related

Laravel Return Response From Ajax

I am trying to learn Laravel9. For this I have Created a Controller, a model and blade view file. Below is my vatprofile.blade.php file code
<script type="text/javascript" src="https://code.jquery.com/jquery-2.0.2.js"></script>
<script>
$(document).ready(function () {
$.ajax({
type: "get",
url: "/vatprofile",
dataType: "json",
success: function (response) {
alert(response);
}
});
});
</script>
And this is my Rout,
Route::get('/vatprofile',
[VatProfileController::class,'index']);
And Below is my Controller Function
public function index(){
return response()->json(['success'=>'Record is successfully added']);
}
It is not Giving Error, Not printing out any response. Only the Blank Page is Showing up. Can you Correct me Pls
Please try this:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.0.2.js"></script>
<script>
$(document).ready(function () {
$.ajax({
type: "get",
url: "{{ url('vatprofile') }}",
dataType: "json",
success: function (response) {
alert(response);
}
});
});
</script>

how to make successfull ajax success

Button code
this is my button code with id 123
<button type="submit" class="addto" id="123">Add to cart</button>
ajax code
This is my ajax code
$(document).ready(function(){
$('.addto').click(function (event) {
event.preventDefault();
$id=this.id;
//alert($id);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: '/test', //route url
dataType:"json",
type:"POST",
data:{id:this.id},
success: function(result){ //success function
alert("success"); //test alert
}});
});
});
Route code
this is my route code
Route::match(['get','post'],'/test','ajaxcontrol#test'); //route url
Controller code:
How to solve this, i have included all the imports
class ajaxcontrol extends Controller
{
public function test(Request $request){
$valu=$request->id;
echo json_encode($valu);
}
}
Try to change your ajax like this:
$(document).ready(function(){
$('.addto').click(function (event) {
event.preventDefault();
id=this.id;
//console.log(id);
$.ajax({
url: "{{ url('/test') }}", //route url
dataType:"json",
type:"POST",
data:{
id:id,
"_token": "{{ csrf_token() }}",
},
success: function(result){ //success function
alert("success"); //test alert
}});
});
});

Getting error in ajax request when calling API

Can anyone tell me that what is the error in my code? When I call my API, it could not able to get success.
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url:"https://www.erp.tpci.in/api/badge/list?authId=indusfood&authPassword=indusfood#123&page=1&company_name=&name=&code=&badge_id=141&buyer_id=1300&limit=10",
type: "GET",
dataType: "json",
success: function(data){
console.log(data);
},
error: function(error) {
console.log('Error');
}
});
});
</script>

MVC Razor Ajax call from button click

I'm struggling to get a button click to invoke an Ajax function which then posts to a controller action. Cannot even get a simple message to work (nothing happens when button is clicked). Clearly, I'm missing something fundamental. What is it?
The Ajax script in my Razor form:
<script type="text/javascript">
$('#UseShipAddr').click(function () {
$.ajax({
url: "#(Url.Action("UseShippingAddress", "Checkout"))",
type: "POST",
data: { id: 50 },
cache: false,
async: true,
success: function (data) {
alert(data);
}
});
});
</script>
The button that I want to use to invoke the Ajax function:
<input type="button" value="Use Shipping Address" id="UseShipAddr" />
The action in CheckoutController:
// Ajax POST: /Checkout/UseShippingAddress/5
[HttpPost]
public ActionResult UseShippingAddress(int id)
{
return Content("It worked!");
}
Please try this code.
$(document).ready(function(){
$('#UseShipAddr').click(function () {
$.ajax({
url: "Checkout/UseShippingAddress",
type: "POST",
data: { id: 50 },
cache: false,
async: true,
success: function (data) {
alert(data);
}
});
});

How to use Ajax in Smarty?

This is ajax request
<script type="text/javascript">
{literal}
$(document).ready(function(){
$("#S1").change(function()
{
var IDCat=this.value;
alert(IDCat);
$.ajax({
type: "GET",
url: 'product_modify.php',
data: {IDCat:IDCat},
success: function(data)
{
alert(data);
alert("success");
}
});
});
});
{/literal}
</script>
And this is php codes
if(isset($_GET['IDCat'])){
$idc= $_GET['IDCat'];
echo $idc;
}
there is the problem echo $idc; doesn't work ? where is the problem ?
var IDCat=this.value;
should be
var IDCat=$(this).val();

Resources