I am trying to make a simple web API call inside an HTML page using ajax (on button click). but that call always fail. Issue is only with ajax (on button click) call for that API. loading in combobox works perfectly fine. So, I hope it should not be a cross domain issue.
Following is the code where it causing trouble:
<body>
<a class="offline-button" href="../index.html">Back</a>
<input id="btnSayHello" type="button" value="Get Value From Ajax" /><br />
<div id="example" class="k-content">
<div class="demo-section">
<h2>Products</h2>
<input id="products" style="width: 250px" />
</div>
<script>
$(document).ready(function() {
$("#products").kendoDropDownList({
//dataTextField: "Name",
//dataValueField: "Name",
dataTextField: "ProductName",
dataValueField: "ProductID",
dataSource: {
transport: {
read: {
dataType: "jsonp",
url: "http://demos.kendoui.com/service/Products",
}
}
}
});
});
$(function () {
$("#btnSayHello").click(function () {
$.ajax({
type: "GET",
contentType: "jsonp",
//contentType: "application/json; charset=utf-8",
dataTypes: "jsonp",
processdata: true,
data: {},
url: "http://demos.kendoui.com/service/Products",
success: function (data) {
alert("Success");
},
error: function (result) {
window.alert(result.responseText);
}
});
})
});
</script>
For button click's Ajax call's responseText it returns "undefined"
Same URL call for Document ready function works fine. and populate that ComboBox.
SOURCE CODE can be found at this link.
Thanks everyone in advance for your help!
you can see this,i think it solves your issue,try to send some duplicate value as queryparam by using callback.
$(document).ready(function(){
$("#btnSayHello").click(function () {
$.ajax({
type: "GET",
url: "http://demos.kendoui.com/service/Products?callback=123",
dataType: "jsonp",
success: function (data) {
alert("Success"+JSON.stringify(data));
},
error: function (result) {
alert("error:"+result.responseText);
}
});
});
});
http://jsfiddle.net/nPeaV/7370/
Okay I got my answer and the problem is because of "dataTypes" keyword. I changed it to "dataType" and it worked.
Well, I am new to web technologies and initially I tried to develop the same code in aspx page where "dataTypes" works just fine. Therefore, I tried to use same code base in a normal HTML file and it didn't work.
So, the conclusion is there might be some different treatment for "dataTypes" in aspx pages as compare to HTML5.
Related
I have implemented ajax function inside java script function as follows
<script type="text/javascript">
function subscriptionLookup() {
$.ajax({
url:'deleteApplication?appId=9',
data: json,
type: "POST",
success: function (response) {
showMessageDialogBox(response);
},
error: function(response) {
showMessageDialogBox(response);
}
});
}
</script>
Then I call this javascript function as follows
<a href="javascript:subscriptionLookup();" data-toggle="modal" data-target="#confirm-delete" href="#" >[Delete]</a>
But it gives nothing for me. I found , this call comes inside javascript function but not going into ajax function.
What is the problem with this
Well, firstly, there is a problem with your code itself. You wrote data: json
In this place, you are supposed to pass information that you want to send to your server. If you want to send the word 'json', you might want to use quotes (just like you did with URL).
If you want to specify the data type that you want to recieve from the server, then you should remove this and instead add dataType: 'json' .
I hope the answer will be helpful.
You have few issue in your code. Check the modifications made.
<a data-toggle="modal" data-target="#confirm-delete" href="#" id='xxxxy' >[Delete]</a>
<script type="text/javascript">
$(document).ready(function(){
$('#xxxxy').on('click',function(){
subscriptionLookup();
});
function subscriptionLookup() {
$.ajax({
url:'deleteApplication?appId=9',
dataType: 'json',
type: "POST",
success: function (response) {
showMessageDialogBox(response);
},
error: function(response) {
showMessageDialogBox(response);
}
});
}
});
</script>
I have the following httpget that calls a controller action:
$.get('/Course/ExplanationCorrect/', postData, function (data) {
$('#SurveyDiv').html(data);
});
This is working on all four other browsers but not on IE10 second pass through. I believe that this is a caching issue and I want to set cache to false. How can I do this?
I have tried the following:
$.get('/Course/ExplanationCorrect/', postData, function (data) {
cache: false,
$('#SurveyDiv').html(data);
});
Using this line of code will set the caching to false for all of your jQuery get requests
<script type="text/javascript">
$.ajaxSetup({ cache: false });
</script>
You can put that anywhere after your jQuery script tag
Alternatively, if you don't want to globally disable caching, you can use the following code for this request
$.ajax({
url: '/Course/ExplanationCorrect/',
cache: false,
data: data,
success: function (data) {
$('#SurveyDiv').html(data);
}
});
I am trying to call a web-service which has a Json output from JavaScript. But I am unable to get the value. I tried with different methods but have been unsuccessful. Please help !!
Here is the code I tried
<script src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function callajax(){
var html =$.ajax({
cache: false,
type: "GET",
async: false,`enter code here`
data: {},
url: "http://domain/abc.php?param=abcd',
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: true,
Success: SucceedFunc,`enter code here`
error: function (request, errorText, errorCode)
{
alert(errorText+"--"+errorCode);
}
});
}
function SucceedFunc(data, status)
{
alert("Enter into Success");
alert(data);
}
Desired output is in {"name":Alex,"Success":true} format.
I need to pick value for "name".
Help would be appreciated.
Is it because your Success: is capitalized? Should be success:.
EDIT:
Also, is there a reason you're using such an old version of jQuery? They're at 1.9.x now (for most uses). I just looked it up and the crossDomain option was added in 1.5.
I have a JSON response coming from REST web service and want to bind that data to html using jQuery. Looks like its not even hitting web service url which I have provided in my jquery.
Url is working fine which gives me JSON data in browser but jQuery I am using unable to get any content from this. I am pasting my code here, plz let me know if some one can help.
While debugging script its directly going on error section in ajax call.
<script type="text/javascript">
$(document).ready(function () {
GetData();
});
function GetData() {
// alert(textblock.value);
$.ajax({
type: "GET",
url: "http://localhost:8092/api/Employees",
data: "{'employeeId'= '" + 1234 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var results = $.parseJSON(msg.d);
alert(msg);
alert(results);
},
error: function (result) {
alert('here');
var tt = result.text;
alert(tt);
}
});
}
</script>
finally i am able to get data now.
I added below properties in $.ajax:
processData: false,
async: false,
I have a mobile app using mostly JQuery Mobile. I have an ajax function using POST and I can't seem to get anything to effect the UI when I fire the click event. I tried setting
$('#cover').show();
as the very first thing in the function then I do some basic things like document.getElementById('user') etc to set some variables and check input, but as long as the ajax function is there it won't show the div or even the spinner from JQ Mobile. Unless I debug and step through the code then the spinner and div show up fine. I tried setTimeout and putting it in the beforeSend area of the ajax call. Everything works fine otherwise. It seemed to work a little better with GET I'm not sure if that has anything to do with it or not.
$.ajax({
cache: false,
type: "POST",
async: false,
url: urlString,
data: jsonstring,
contentType: "application/json",
dataType: "json",
success: function (data) {
JSONobj = JSON.parse(data);
},
beforeSend: function(xhr){
//console.log('BeforeSend');
},
complete: function (xhr) {
//console.log('Complete');
},
error: function (xhr, status, error) {
console.log(error);
}
});
You could use the Ajax Global handlers to handle this:
$(document).
.ajaxStart(function(){
$('#cover').show();
})
.ajaxStop(function(){
$('#cover').hide();
});
This way you don't have to worry about showing/hiding the overlay on individual Ajax calls.
Try this
$("#someButton").click(function(e){
e.preventDefault() //if you want to prevent default action
$('#cover').fadeIn(100,function(){
$.ajax({
url: "someurl",
data: "Somedata",
contentType: "application/json",
dataType: "json",
},
success: function (data) {
JSONobj = JSON.parse(data);
$('#cover').fadeOut(100);
},
complete: function (xhr) {
$('#cover').fadeOut(100);
}
});
});
});