I am trying to make a request to a backend API I created on Google App Engine. Right now it should be pretty simple, it sends the URL, and what should be returned is JSON that looks like this {"keys": [5676073085829120]}. I have tested the API by making CURL requests, and the the URL works, one thing that confuses me is that when I make a CURL request I have to specify "Accept: application/json", but I do not know how to add that to a getAsync request. Here is the code in question:
function verify(){
var uname = document.getElementById("username").value;
var pword = document.getElementById("password").value;
var c = new Windows.Web.Http.HttpClient();
var complete = "http://golden-bonsai-124817.appspot.com/users/" + uname + "/" + pword;
c.getAsync(new Windows.Foundation.Uri(complete)).done(function (result) {
var jsonResult = JSON.parse(result.content.toString());
var key = jsonResult.Results.series[0].data;
console.log("in here");
var authKey = new Array();
key.forEach(function (cur, i, arr) {
authKey.push(cur.keys);
});
};
I tried stepping through the code with the debugger in visual studio. It initializes the variables, and the value of my 'complete' variable is the correct URL that I have used for my cURL requests. I set a breakpoint inside of the function that is supposed to happen once the request completes, but the code never makes it inside of that function and eventually the windows phone emulator goes black and it seems like it just hangs, it doesn't exit but it gets to a point where I can no longer step through. I have been trying and trying but I just can't figure it out, and to make it worse the documentation for all of this stuff is garbage. Any help would be very greatly appreciated. Thanks in advance.
That need to be specified in the content of a request. To specify the content type of a request, you need to use HttpRequestMessage to create request and then specify the media type. You then need to use sendRequestAsync method of HttpClient to process your request. So your code will be something similar to the following.
var hc = new Windows.Web.Http.HttpClient();
var uri = new Windows.Foundation.Uri("http://golden-bonsai-124817.appspot.com/users/" + uname + "/" + pword);
var request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.get, uri);
var content = "";
var encoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
var mediaType = "application/json";
request.content = new Windows.Web.Http.HttpStringContent(content, encoding, mediaType);
hc.sendRequestAsync(request).then(...);
Related
I am writing a program using JavaScript, which connects to a local ArcGIS server through ArcGIS REST API and loads the maps.
The URL to get service is
let xmlhttp = new XMLHttpRequest();
xmlhttp.withCredentials = true;
let url = "http://domain/arcgis/rest/services/" + serviceName +"/MapServer/" + layer_id + "/query?f=json&where=1%3D1&returnGeometry=false&outFields=*";
I can get the token through the API. The problem is, I can't set it in cookies, as the browser rejects programmatically writing another domain.
May someone suggest a workaround?
You can add &token=abcd1234 to your url, so you get :
let token = 'abcd1234';
let url = `http://domain/arcgis/rest/services/${serviceName}/MapServer/${layer_id}/query?f=json&where=1%3D1&returnGeometry=false&outFields=*&token=${token}`;
I am wondering if it is possible to make an API call from within a design doc. I have tried the code below, however I am getting the following error message.
{"error":"forbidden","reason":"CSRF Cookie/Header mismatch"}
This is the code:
function(head, req) {
var id = req.query.id;
var contactName = 'This is the new contact name!!';
var sendString = '{"PrimaryContactName":"' + contactName + '"}';
var xhr = new XMLHttpRequest();
xhr.open('PUT", <URL>, false);
xhr.send(sendString);
var sendStatus = xhr.status;
}
Thanks!
You aren't going to be able to use AJAX from CouchDB. (it's not a web browser)
If you want changes in your database to be propagated to other data-sources, you can use the _changes feed. That will be a much more robust solution no matter how you slice it.
I am not sure if what I'm trying to do is possible or if I'm going about this the right way. In some circumstances I want them to have a GET parameter as part of the URL. I want the receiving page to be able to differentiate whether the sending load has a parameter or not and adjust accordingly.
Here is what I have that is sending the load:
$(document).ready(function () {
$("a").click(function () {
$("div.pageContent").html('');
$("div.pageContent").load($(this).attr('href'));
return false;
});
});
In this case, the load could have "example.php" or "example.php?key=value". In looking around (primarily on this site), I've found things that seem to be close, but don't quite get there. In the page that is getting loaded (example.php), I have the following:
function $_GET(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
$(document).ready(function () {
var URL = "example2.php";
if ($_GET('key'))
{
URL = "example2.php?key=" + $_GET('key');
URL = URL.split(' ').join('%20');
}
$("div.output").load(URL);
});
If the sending source includes a query string, I want to add that to the URL and load it in a div that is unique to this page, otherwise I want to just load it as is without the query string. The big issue I'm running into (I believe) is since this is coming from an AJAX call, the "window.location.href" is not what was sent from the JQuery but rather the URL of the root page which never changes. Is there a way to be able to know what the full URL is that was sent from the load() in the first page by the second one?
Thank you in advance for your help.
I realized that the GET parameters were getting passed as I could access them through php without issue. I didn't know that I could insert php code into a javascript block but once I tried it, all worked out. My new code looks like this:
$(document).ready(function () {
var URL = "example2.php";
var myValue = "<?php echo $_GET['key']; ?>";
if (myValue !== "")
{
URL = "example2.php?key=" + myValue;
URL = URL.split(' ').join('%20');
}
$("div.output").load(URL);
});
I was able to get rid of the GET function out of javascript entirely. I probably made this much more difficult from the start but hopefully it can help someone else in the future.
First off, I am very new to web services, web workers, and XMLHttpRequests, so please bear with me. Also, there are a lot of stipulations in my project, so solutions to "just do it this way" may not be viable.
So I have a web service set up to receive calls from an XMLHttpRequest in javascript, and it does this synchronously. This works fine, but it ties up the UI thread, and I would like to have a loading spinner run while making the requests to the server. Due to one issue, I can't have the program access external scripts on the web, so I am using a Blob to mask the "file://" preface.
I am also using an inline webworker to accomplish this. Now I'm getting to my actual issue. Spawning the webworker is fine, and I can create and send the XMLHttpRequest, but as soon as I call "send" everything exits. It will run no lines of code after this.
Here's some code:
Called from JS:
var blob = new Blob([document.querySelector('#getWorker').textContent]);
var blobUrl = window.URL.createObjectURL(blob);
var worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = function (e) {
alert(e.data);
}
worker.postMessage();
The worker:
var bigString = "";
var invocation = new XMLHttpRequest();
var url = 'http://<ipAddress>/<serviceName>/Service.asmx/<method>';
if (invocation) {
invocation.open('GET', url, false);
invocation.send(); //*****EXITS AFTER THIS LINE*****//
if (invocation.status == 200) {
var responseText = invocation.responseText.replace(/(\r\n|\n|\r)/gm, "");
responseText = responseText.replace('<?xml version="1.0" encoding="utf-8"?>', '');
responseText = responseText.replace('<string xmlns="http://tempuri.org/">', '');
responseText = responseText.replace('</string>', '');
postMessage("Success");
//updateTable(responseText);
} else {
postMessage("Fail");
//alert("Could not connect to database. Check your internet connection.");
}
var c = 0;
var b = 1;
}
var q=1;
The debugger will just end after the "invocation.send()" line. No error, no status, no nothing. And that's where I'm lost.
Any insight would be greatly appreciated. Also, this exact code works when it is not in a WebWorker, so there's likely something about them that I do not understand.
Thanks in advance!
Chrome problem. Silent fail if COR request is blocked. Firefox console shows more information on the error.
I added variables in the request as per the Microsoft standard below, var openRetVal and var sendRetVal... Odd thing is, that they dont get anything returned in them, so did Microsoft lie in their own documentation?
I was working on a ajax request, and like usual, IE is a difficult specimen to work with. I found that instead of doing a AJAX request, i can do an XDR. My code in chrome works, so i know the destination server is working and on a successful request does what is suppose to happen. Below is my code segment for an XDR.
if ($.browser.msie && window.XDomainRequest) {
var xdr = new XDomainRequest();
//var webstring = location.protocol +"//"+ location.host +"/" + WEBSERVICE_URL + "/test";
//WEBSERVICE_URL = "webservices/FormDesigner.svc";
var webstring = WEBSERVICE_URL + "/test";
var openRetVal = xdr.open("GET", webstring); //added this var as it supposidly gets a return value from the function call.
xdr.onload = function () {
var JSON = $.parseJSON(xdr.responseText);
if (JSON == null || typeof (JSON) == 'undefined') {
JSON = $.parseJSON(data.firstChild.textContent);
}
//below is my onsuccess call which is called by both successes for IE and NON-IE processes allowing all stuff to be piped into 1 call.
ajax_success(JSON);
};
xdr.ontimeout = function () {
alert("XDR Error. Timeout");
}
xdr.onerror = function () {
alert("XDR Error. Unable to do a Cross Domain Server Request.");
};
var sentRetVal = xdr.send(); //added this var as the function is suppose to return success or error as per microsoft.
}
It always returns onerror which is NOT what i am aiming for, naturally. I am pinging something within the same domain for the moment for testing purposes which is why there is not other stuff. Like i said, it works with other browsers so far... Is there an improper formatting I am unaware of? There is no data submitted as well with this test request.
If you are already using jQuery, just use jQuery for ALL BROWSERS, then you should not have any issues in IE.