i make app ios using xamarin.ios and use webview
in .chtml file call webservice using ajax as following code
<script>
$(document).ready(function () {
$.ajax({
type: "GET",
url: 'http://www.chc-sa.com/webService/AboutUs',
error: function (xhr, status, error) {
//do something about the error
document.getElementById("net").textContent = "Network Error";
},
success: function (response, $Id) {
//do something with response
document.getElementById("details").innerHTML = response.Details.replace(/(?:\r\n|\r|\n)/g, '<br />');
}
});
//-----------------------------------
});
</script>
code always fire error not excute success
and code work fine in xamarin.android or windowsphone
but in xamarin .ios when run it in simulator mac show error only
is there some configuration in xamarin.ios to execute ajax code correctly? or in iphone mobile?
Related
I am developing an app for BlackBerry 10 using cordova.
Since I updated my BlackBerry OS from 10.2.1 to 10.3.1 I have the following problem: When I do a Ajax Post with JSON I don't get a data result, but step into the success function.
My code looks like this:
window.jQuery.ajax({
url: "http://myurl.com/systemservice.asmx/ServiceName",
data: "{'DataName':'DataValue'}",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
//Do something else with data.d
}});
The alert returns "null".
Testing the App in a web Browser (also build it as Browser App with cordova) works fine. Even with the old Version of BlackBerry OS it worked fine. Any ideas?
I have a couple of my Cordova apps running on 10.3.1 and they seem to still work fine... though I'm using Angular's $http to make the calls.
I would start by trying to build the app with WebInspector enabled (use the WebWorks SDK GUI, or it's the -d flag while building, I think) to check the console for errors. Or use this tidbit to echo console output into alert messages:
var _log = console.log,
_warn = console.warn,
_error = console.error;
console.log = function() {
alert(JSON.stringify(arguments));
return _log.apply(console, arguments);
};
console.warn = function() {
alert(JSON.stringify(arguments));
return _warn.apply(console, arguments);
};
console.error = function() {
alert(JSON.stringify(arguments));
return _error.apply(console, arguments);
};
Good luck!
I am making hybrid application using IBM Worklight.
In my windows phone 8.1, when I am running this application, ajax calls returns readyState 4, status 404.
Am I doing something wrong? Any help would be greatly appreciated.
Screen Shot of Project Files Hierarchy Here
AJAX Request Code:
$.ajax({
type: "get",
url: "index.html",
success: function (data) { alert("success" + JSON.stringify(data)); },
error: function (error) { alert("error" + JSON.stringify(error));}
});
You have to append "www/default" before page name, because In windows phone MainPage.xaml is loaded first, which is in root directory. And $.ajax will search from root directory and hence you have to give path as below.
$.ajax({
type: "get",
url: "www/default/index.html",
success: function (data) { alert("success" + JSON.stringify(data)); },
error: function (error) { alert("error" + JSON.stringify(error));}
});
If your application have too many $.ajax or $.get and you don't want to modify each and every request you can use following global ajax settings when your application starts.
$.ajaxSetup({
beforeSend: function (jqXHR, settings) {
settings.url = "www/default/" + settings.url;
}
});
This chunk of code works fine on FireFox but in IE it gives runtime error when I try to dump the contents from response returned from AJAX response.I am using mootools to make Ajax call.
//on dom ready...
window.addEvent('domready', function() {
/* ajax alert */ <br/>
$('ajax-alert').addEvent('click', function(event) {
//prevent the page from changing
event.stop();
//make the ajax call
var req = new Request({
method: 'get',
url: $('ajax-alert').get('href'),
data: { 'do' : '1' },
onRequest: function() { alert('Request made. Please wait...'); },
onComplete: function(response) {
alert(response);// Getting response and able to see that in alert
/*line underneath fives runtime error in IE , works fine in FireFox */
document.getElementById('myDiv').innerHTML = response;
//this line gives run time error on IE
}
}).send();
});
This is likely to happen when the returned response is not a valid X/HTML. I suggest you validate the page with the response returned in it. As Dimitar Christoff has mentioned, watch out for inline elements containing block elements.
I need to load an image if if hasn't been loaded yet.
For this i'm using the error function like this:
$.ajax({
type: "POST",
url: "inc/functions.php",
data: { productID: productIDVal, action: "addToCart"},
success: function(theResponse) {
busy=false;
$('#buyButton')
.error(function(){
var t = $("<img id='buyButton' src='images/checkout.png' />");
$.append(t);
});
}
});
But it is not working. Am i doing something wrong here?
Thanks in advance.
You need to specify where to append the image:
$('#buyButton').append(t); // NOT $.append(t);
// OR $(this).append(t);
More info here
Some general tips on debugging javascript.
Quick and dirty: Put an alert message on the first line of the error function like alert('inside error'). Then load the page and see if the alert message shows up. You can put variables inside the alert message to see what their values are. If you don't see an alert message it means that the code is not even being loaded for some reason, so you have to put an alert message at an earlier point. (This can get very tedious).
Better way: Start using Firebug or Safari's Web Inspector to debug the javascript. Just put debugger anywhere in your code and when the browser gets to that line of code, it will stop and give you a console with access to all variables and functions available at that point in the code.
The problem may be with the AJAX request. See what it is returning by trying this code:
$.ajax({
type: "POST",
url: "inc/functions.php",
data: { productID: productIDVal, action: "addToCart"},
success: function(data){ alert('success!'); },
error: function(XMLHttpRequest, textStatus, errorThrown){ alert(errorThrown) ;}
})
You can replace the alert messages with debugger to do further inspection of what is going on with Firebug.
you should move the code to error block
remove from the success block
error: function(request,error) {
var t = $("<img id='buyButton' src='images/checkout.png' />");
$(this).append(t);
}
the skeleton goes like this
$.ajax({
},
beforeSend: function() {
},
error: function(request,error) {
var t = $("<img id='buyButton' src='images/checkout.png' />");
$(this).append(t);
},
success: function(request) {
} // End success
}); // End ajax method
I just begin to learn jquery ajax framework here is my first attempt:
<div>
<input id="ajax" type="button" value="Read" />
</div>
also the js code:
$("#ajax").click(function () {
$.ajax({
type: "get",
url: "http://www.111222333444555.com",
//url: "http://www.google.com"
// timeout: 2000,
success: function () {
alert("ajax success!");
},
error: function () {
alert("ajax failed!");
}
});
});
abviously the "http://www.111222333444555.com" is not accessible,so I consider that resault is alert the "ajax failed!", but the resault is that the success function be executed,which alert "ajax success!"
then I change the url to "http://www.google.com",it alert "ajax success!" as well,
why the accessible url could cause the success function ?how can I escape the situation?I want when the url is not accessible,it will auto execute error function?How the $.ajax exactly works?
Please help me , thank you very much
Using the built-in objects, you cannot use Ajax to request a page that is outside of your page's domain.
There are solutions however...