$.getJSON to $.ajax syntax? - ajax

I have this working .getJSON
$.getJSON('data3.json', function (data) {
})
If convert to $.ajax What should be the value for data?
$.ajax({
type: 'GET',
url: 'data3.json',
datatype: 'json',
data: ?,
success: function(data) {
}
});

You should try this
In the ajax, they are not compulsory to pass some parameters like data, datatype etc.
So you can call ajax without data parameter
$.ajax({
type: 'GET',
url: 'data3.json',
datatype: 'json',
success: function(data) {
}
});

Related

ASP.NET Core FromBody is null on AJAX POST but populates in Postman

I'm trying to perform an AJAX post but I keep getting a null FromBody in my .NET controller. I think it has to do with how I'm formatting my AJAX post.
When I attempt to post with AJAX I get a null FromBody.
var data = {
Date: "2016-12-01",
BurnIdx: 23,
BurnStatIdx1: 3,
BurnStatIdx2: 3,
BurnStatIdx3: 3,
BurnSevIdx: 5,
WorkOrder: 32426,
Comment: "Hi"
};
$('#submit').on('click',function () {
$.ajax({
type: 'POST',
url: 'Home/BurnerMapUpdate',
dataType: 'json',
contentType: 'application/json',
data: data,
success: function (result) {
console.log('Data received');
console.log(result);
}
});
});
However, when I attempt a post in Postman it's successful.
Figured out my problem. Needed to use JSON.stringify on my data.
$('#submit').on('click',function () {
$.ajax({
type: 'POST',
url: 'Home/BurnerMapUpdate',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(data),
success: function (result) {
console.log('Data received');
console.log(result);
}
});
Not only the JSON.stringify().
If your data don't match with csharp class data it doesn't receive anything.
Like if you define in the class a field with int type, and send it in the json like "1", it happens to receive the whole data as null
I know you are already figured out of this problem. But I faced with the same just right now. My mistake was I used BODY parameter instead DATA in ajax request.
My Invalid ajax:
function sendRequest(url, method, body, callbackOk, callbackFail) {
$.ajax({
url: url,
body: JSON.stringify(body),
method: method,
contentType: 'application/json; charset=utf-8',
success: callbackOk,
error: callbackFail
})
Valid:
function sendRequest(url, method, body, callbackOk, callbackFail) {
$.ajax({
url: url,
data: JSON.stringify(body),
method: method,
contentType: 'application/json; charset=utf-8',
success: callbackOk,
error: callbackFail
})
}

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);
};

AJAX call return undefined

This AJAX call is returning "undefined". I'm not sure what I'm doing wrong and why this isn't working:
var xmlfile;
$.ajax({
type: 'GET',
url: 'sample.xml',
dataType: 'xml',
success: function(data){
xmlfile = $(data);}
});
console.log(xmlfile);
This is likely to be a timing issue since you're referring to the xmlFile variable before the call returns. Instead you have to move the reference into the success callback.
$.ajax({
type: 'GET',
url: 'sample.xml',
dataType: 'xml',
success: function(data){
xmlfile = data;
console.log(xmlfile);
}
});
Try the above.
you can do this by
$.ajax({
type: 'GET',
url: 'sample.xml',
dataType: 'xml',
success: function(data){
xmlfile = data ;}
});
or set the async : false,
you can detect error/problem by debugging so you can see where are you doing wrong
like see alert(data) it it does it mean you are getting successful response by ajax call
var xmlfile;
$.ajax({
type: 'GET',
url: 'sample.xml',
dataType: 'xml',
async : false,
success: function(data){
xmlfile = $(data);}
});
console.log(xmlfile);
try This, you are getting undefined due to asyn call, your log executes before you got result from server

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);
}
});

Get response from url using ajax and jquery

want to get response from url using ajax and jquery.
tried with this code
$(document).ready(function () {
$.ajax({
type: 'POST',
url: 'apexweb.co.in/apex_quote/uname_validation.asp?,
dataType:'jsonp',
success: function(data){
alert(data);
}
});
});
i want to display response as fail but i didn't get any response on browser
Help Me
Try this
$(document).ready(function () {
$.ajax({
type: "POST",
url: "apexweb.co.in/apex_quote/uname_validation.asp?",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function (data) {
alert(data.d);
},
failure: function (data) {
alert(data.d);
}
});
});

Resources