Not able to make ajax request in windows phone phonegap - ajax

I have developed android app using phonegap. Now I am trying to use that code for developing windows phone 8 app. I have successfully set up the Visual Studio Express environment and made Cordova Template. After that I have created the project and inserted my assets into that project.
It shown me my app UI perfectly and events, page navigation are working fine. But it is not able to make ajax http connection. Do I need enable any thing in SDK or Project Directory.
When am I trying this , I am getting alert that 'Connectivity Failure'
This is my sample js code for making http connection
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg("a","Processing !!! Please wait "); },
complete: function() { $.mobile.hidePageLoadingMsg(); },
type: 'POST',
url:'http://192.168.12.175/Receiver.php',
data: reqObj ,
success: function(data){
responseString=data;
}
error: function(data){
//get the status code
alert('Connectivity Failure');
} });

use this one
$.mobile.showPageLoadingMsg("a","Processing !!! Please wait ");
formData = {
param1: param1
}
$.ajax({
type: 'POST',
contentType: 'application/json',
url: "http://192.168.12.175/Receiver.php",
dataType: "json",
data: formData,
success: function(data) {
$.mobile.hidePageLoadingMsg();
},
error: function(data) {
alert('Connectivity Failure');
}
});

Adding
<feature name="http://api.phonegap.com/1.0/network"/>
in config.xml fixed the issue for me.
This is adding the <Capability Name="ID_CAP_NETWORKING" /> into WMAppManifest.xml file when build is generated

Related

cordova apk doesnt send ajax request

I have developed an app using cordova(version 6.3.1). I have checked it on nexus 5 and nexus 6P and everything looks fine, but i tested it on genymotion emulator and galaxy S6, but they didn't send any request(I checked server logs).
Does it anything to do with security problems? Or there is another problem?
The ajax POST method is the code below.
$("#login").click(function() {
$.ajax(serverUrl + "/login", {
data: {
email: userEmail,
password: userPassword
},
dataType: "json",
method: "POST",
statusCode: {
200: function(data) {
//OK, Welcome!
$("body").pagecontainer("change", "#welcomePage");
},
}
});
});
Can you show us your part of source code where you have this problem ?
Did you check you have this plugin in your project ?
cordova-plugin-whitelist
Also, can you throw us your HTTP response when you're sending your AJAX request to your server
It may be, something like this :
$.ajax
({
contentType: "application/json; charset=utf-8",
dataType: "json",
data: ...,
url: ...,
type: 'POST',
timeout: 5000,
success: function (data)
{
},
error: function(msg)
{
alert("Error:"+msg.status + msg.responseText);
}
});

File deleted before downloaded in Ajax call in Asp.net MVC

I am generating a excel file by using the Ajax call to my Action in my controller class in my ASP.net MVC application.Its working fine but the problem occures some time when my file is in downloading stage and the ajax call delete it.If there is any way without SetTimeout then please tell me.
Generate Excel File
$.ajax({
url: "#Url.Action("GenerateReport", "ClientAdmin")",
type: "POST",
data: { reportStart: reportStart, reportEnd: reportEnd},
dataType: "json",
traditional: true,
success: function (downloadUrl) {
//Download excel file
window.location = "/ClientAdmin/Download?file=" + downloadUrl;
//Delete excel file
$.ajax({
url: "#Url.Action("DeleteReportFile", "ClientAdmin")",
type: "POST",
data: { file: downloadUrl },
dataType: "json",
success: function (downloadUrl) {
},
error: function () {
AlertShow("Error!", "Oops! An error occured");
}
})
},
error: function () {
AlertShow("Error!", "Oops! An error occured");
}
})
I think you need jQuery "when".
http://api.jquery.com/jQuery.when/
$.when( $.ajax( "test.aspx" ) ).then(function( data, textStatus, jqXHR ) {
alert( jqXHR.status ); // Alerts 200
});
this is to force something to be synchronous.
(One can find the credited answer here)
EDIT
as I am not sure how to test your case I might suggest another solution.
Try handling "file downloaded" event somehow and then trigger the delete function.
Here is a possible useful answer and blog.

ajax call back readyState 4, status 404 in windows phone 8.1

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

How to consume a rest service hosted on remote server in asp.net mvc3 using jquery ajax ?

I have a rest service. I hosted this service to IIS in my local system and consumed in an mvc3 application. It run without any error. But in the same application, I am not able to consume the rest service hosted on remote server. I have written following jquery to access it :
<script type="text/javascript">
$(document).ready(function () {
$("#btn").click(function () {
$.ajax({
url: "http://portno/Student.svc/AutoComplete?Branch=Civil&RecordLimit=4&s_name=Linda",
type: "GET",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "xml",
success: function (data) {
alert(data.documentElement.childNodes[3].textContent);
},
error: function (error) {
alert("Error: " + error.text);
}
});
});
});
is there any mistake here? Please help me. Thank you.

In MVC calling webservice with ajax call not working give error code 404?

In my .net framework with MVC calling webmethod like. webservice1.asmx/helloWorld
with Ajax give error 404 not found..In my another server same code working. Is there
anything missing to call ?? and physical path give me same webserive and webmthod in my .net project.. please help me ..
EDIT
code to call the web service
$.ajax({
type: "POST",
url: "/WebServices/WebService1.asmx/HelloWorld",
data:"{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(msg) {
var data = msg.d;
},
error: function(msg) {
alert(msg);
}
});
I suspect that you have hardcoded the url to the web service in your javascript call instead of using an url helper to generate it. So try like this:
<script type="text/javascript">
$.ajax({
type: "POST",
url: "#Url.Content("~/WebServices/WebService1.asmx/HelloWorld")",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(msg) {
var data = msg.d;
},
error: function(msg) {
alert(msg);
}
});
</script>
Notice how the url to the web service is no longer /WebServices/... but it is generated with an url helper. So if for example you deploy your application in a virtual directory in IIS the helper will take into account this virtual directory.

Resources