I'm working on this TaskPane app for office, and I would like to get some data from a php function that resides on our domain.
We're using the same php function in other apps, where I can just call it with some parameters passed to it in the URL, and it returns a simple answer.
Example: https://www.ourdomain.com/phpfunction.php?message=HELLO
The problem is, that I cannot seem to call this function via AJAX, from the TaskPane app, while debugging.
I added our domain to the App Domains in the application manifest, but it didn't help
Here is my AJAX call
function httpGET(theUrl, callback) {
var dataToReturn;
$.ajax({
type: 'GET',
url: theUrl,
cache: false,
async: true,
data: "",
success: function(data, textStatus, result) {
callback(data)
},
error: function (result, textStatus, errorThrown) {
//ALLWAYS GOES HERE
}
});
};
I want to do it in pure javascript, so if it's possible, I don't want to use the custom .NET web service fix for this.
PS: I want to specify, that this exact same call is working perfectly from a mobile app or a browser based app. It's Office who's doing something here...
Related
I have beforeunload event in js which will hit the .asmx service method as provided below.
.js event
$(window).on("beforeunload", function () {
var d, str;
str = '{Id:"' + $('#hdnId').val() + '"}';
d = str;
$.ajax({
type: "POST", //GET or POST or PUT or DELETE verb
url: "../POC.asmx/fUpdateTimeInterval",
data: d,
contentType: "application/json; charset=utf-8",
dataType: "json", //Expected data format from server
async: true,
beforeSend: function () {
// BlockUI();
},
success: function (data, Type, xhr) {//On Successfull service call
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
},
complete: function () {
},
failure: function () {
}
});
});
.asmx (Web Service)
[WebMethod(true)]
public int fUpdateTimeInterval(String Id)
{
return new MY_POC.DLL.Survey().fUpdateTimeInterval(Id);
}
The above service will then call the below mentioned method defined in DLL class file.
public int fUpdateTimeInterval(Int32 Id)
{
List<SqlParameter> objParam = new List<SqlParameter>()
{
new SqlParameter{ParameterName ="#Id",Direction=ParameterDirection.Input,DbType=DbType.Int32,Value= Id},
};
MY_POC.DLL.SqlHelper.ExecuteNonQuery("MY_UpdateTimeInterval", System.Data.CommandType.StoredProcedure, objParam.ToArray());
return 0;
}
Now problem is when the page gets load on browser for first time I am getting current auto ID of the inserted row. If I refreshes the browser, then beforeunload event gets fired and update the row of received ID only. But if I close the tab or browser then the compiler would hit the service method and stops after opening brace, it does not executing further & not even showing any error.
After execution stops, I am getting the following message at Output screen of vs, but not showing any error.
It sounds like execution of the request is being aborted because the browser is closing the connection.
You should consider using the Beacon API. It's well supported by almost all browsers and it's made for this purpose. From Mozilla's documentation:
The main use case for the Beacon API is to send analytics such as
client-side events or session data to the server. Historically,
websites have used XMLHttpRequest for this, but browsers do not
guarantee to send these asynchronous requests in some circumstances
(for example, if the page is about to be unloaded). To combat this,
websites have resorted to various techniques, such as making the
request synchronous, that have a bad effect on responsiveness. Because
beacon requests are both asynchronous and guaranteed to be sent, they
combine good performance characteristics and reliability.
You can also make your Ajax request synchronous to prevent the connection from closing but that will have an impact on your GUI as it will block until the request completes.
I have some html and javascript code running on a WIFI module that runs a webserver. At a certain point, a jquery ajax call is done like so:
reqData = JSON.stringify({FileName: L_JSON_FILENAME, CmdArr: [["read", "cmduart", "data"]]});
jQuery.ajax('' + _szFileRequest,
{
data: reqData,
dataType: 'json',
contentType: "application/json",
async: false,
type: 'POST',
success: function (data, textStatus, jqXHR) {
ReturnedColumn = handleData(data, textStatus, jqXHR);
},
error: function (jqXHR, textStatus, errorThrown)
{
ReturnedColumn = "error";
}
});
Now I want to do the same thing on an android app.
I am currently working in Android Studio.
I have read something about a JSONobject and using toString.
But I have also read about using AsyncHttpClient and using RequestParams.
What is the best way to accomplish this call?
I use Retrofit :
https://square.github.io/retrofit/
It's perfect to make HTTP calls, and it has converters, to ease conversions from JSON, XML, and other format to java
Some examples : https://guides.codepath.com/android/Consuming-APIs-with-Retrofit
Since the webpages were on a webserver I only needed to access that webserver. Connecting to it's WIFI and have a webview open the webpage on the webserver did the trick for me.
The JavaScript runs on the webserver and can easily execute the ajax calls.
I have a (Spring-powered) Java application that has a some AJAX calls. The problem is that I'm using the application context (/spring-mvc) to be able to reach the server-side functionality.
var api = '/api/v1';
var context = '/spring-mvc' + api;
$.ajax({
type: 'GET',
url: context + '/users/' + $('#user-id').val()
}).done(function (response) {
callback({ data: response.user, binding: response.binding });
}).fail(function (jqXHR, textStatus, errorThrown) {
callback({ jqXHR: jqXHR });
});
Now, my problem is whenever I have to deploy it somewhere else, and I can't control the application context, the AJAX calls are eventually failing since the application context itself is hard-coded in the JavaScript.
Is there any way to achieve this without having to change the context variable in all JavaScript files? Something like:
$.ajax({
type: 'GET',
url: '/api/v1/users/' + $('#user-id').val()
}).done(function (response) {
callback({ data: response.user, binding: response.binding });
}).fail(function (jqXHR, textStatus, errorThrown) {
callback({ jqXHR: jqXHR });
});
I've seen that several times in many applications, but I can't figure it out how they do that.
Note: The Web application is deployed in the same WAR file; I would like to avoid to try to figure it out by doing URL manipulation in JavaScript.
Deadly simple, you may include the context with a hidden input, such as
<input type="hidden" id="ctx" name="ctx" value="<applciation_context_here>"/>
then in your js
var context = document.getElementById("ctx").value/*getAttribute('value')*/ + api;
or better way acquire the current windows location(path), and some string manipulation to get the context
update0:
FACTS:
Let say you will have /a , /b and /c contexts, I don't know but usually they will be mapped to a.com , b.com and c.com and context will be identified by the server as requested host, such as following
a.com -> local_host/a
b.com -> local_host/b
But if you are not going as above, as you are generating the jsp file, you may set the context path as hidden input approach
I'm trying to load a RSS feed using Google's Feed API which gives me a JSON string.
(documentation: https://developers.google.com/feed/).
However, I'm trying to use jQuery's AJAX instead of vanilla JavaScript XHR.
It is not working for some reason, which I can't identify why.
Loading the URL in the browser works, however (get the link in the code below).
I have prepared a jsFiddle: http://jsfiddle.net/gberger/fNwpD/
$.ajax({
url:'http://ajax.googleapis.com/ajax/services/feed/load?hl=ja&output=json-in-script&q=http%3A%2F%2Ffeeds.gawker.com%2Flifehacker%2Ffull&v=1.0&num=3',
success: function(data){
alert(JSON.stringify(data));
},
error: function(error){
alert(this.url);
alert(JSON.stringify(error));
}
});
Simply add dataType: 'jsonp' to your options object. Your code is not working because of the Same-origin policy. JSONP is one way of dealing with this if the server supports it (Feed API does).
$.ajax({
url: 'xy',
success: function () {},
error: function () {},
dataType: 'jsonp'
});
Your working fiddle
I am trying to login to a website using a known username and password and to get some data displayed from the site for a specific user account on that website. I am using jQuery and Ajax for this purpose. This is my code:
$.ajax({
async: false,
cache: false,
type: 'POST',
dataType: 'json', // json...just for example sake
data: ({
'login_username': username,
'secretkey': password
}),
url: 'https://mail.someserver.com/src/redirect.php',
success: function (data) {
alert("SUCCESS!")
if (data === '1') { // server returns a "1" for success
// success!
// do whatever you need to do
} else {
// fail!
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
// something went wrong with the request
alert("Failed!");
}
});
I've already made my search around the web and I know that browsers do not permit cross server ajax calls to prevent security issues, but I've already tried to use "jsonp" as dataType to no avail :(
So, what am I doing wrong?
Be sure that your url is not breaking the same origin policy -- that is, the request coming from the client cannot request data from a server from a different domain (there are exceptions to this rule, namingly CORS, but that requires that you make changes to the server/application you're talking to).
The solution to your problem would be to make the request from some server-side script, then in turn having your client application query that script, based on the same machine that's serving the application to the web.
My fault isn't at the code above, my fault was that in my manifest file (I am building a Google Chrome extension) I didn't have set proper permissions (https://*).
Sorry for the frustration!