JSON URL not working with Parameters - javascript

I have written the following code to read a JSON document from an external URL. This worked fine when the URL was the following:
http://localhost/EWSimpleWebAPI/odata/Users?
But NOT when I modified the URL as the following:
http://localhost/EWSimpleWebAPI/odata/Users?$filter=USER_NAME%20eq%20%27corpnet\anuwlk%27&$select=PROFILE
Javascript
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost/EWSimpleWebAPI/odata/Users?$filter=USER_NAME%20eq%20%27corpnet\anuwlk%27&$select=PROFILE";
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
errorAlert("Status OKAY");
} else{
errorAlert("Status Not OKAY")
}
}
xmlhttp.send();
I'm retrieving the JSON Document thru a Web API using OData. OData accepts parameters in the URL and it worked fine in POSTMAN. I'm developing a Google Chrome extension and I'm not sure if it supports this URL with Parameters.

It would be best to use some function ( encodeURIComponent(str) and encodeURI(str) come to mind) to encode the parameters correctly.
As wOxxOm commented, your issue seems that one of the parameter has an unescaped character \.

Related

"request sent by the client was syntactically incorrect" when making REST call to Jira

I'm using HTML macros in confluence to automate some tasks in Jira. I tried the following REST call in the code below to add a comment to a ticket and I get the error: 400: Bad Request. The request sent by the client was syntactically incorrect.
I can't see any issues with my JSON or code. This pretty much fails for other operations I've tried such as creating/updating tickets.
xmlhttp = new XMLHttpRequest();
var url = "http://<CONFLUENCEURL>.com:8090/plugins/servlet/applinks/proxy?appId=<APPID>&path=http://<JIRAURL>:8080/rest/api/2/issue/TEST-3/comment";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.onreadystatechange = function () { //Call a function when the state changes.
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}else{
console.log(xmlhttp.responseText);
}
}
var parameters = {
"body" : "Hello world!"
};
function addComment() {
xmlhttp.send(JSON.stringify(parameters));
}
I eventually created an identically ordered JSON object, turns out that the parser the server side uses does not follow the JSON specification in that it expects the correct order.
As in, the fix would be to follow the JSON specification server side, or replicate identically the structures being sent to the server.

chrome extension http request to website

I wanted to make a chrome extension that gets the html code from this website: https://free-proxy-list.net/
without actually going to that webpage.
I tried using the steps here:
https://developer.chrome.com/extensions/xhr
but the request kept showing as undefined when I tried to print it out.
The script I was using was:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
alert(xhr.responseText);
}
}
xhr.open('GET', "url", true);
xhr.send(null);
document.write(xhr.send());
Note: not putting url in code since SO won't let me post a question with more than 2 links.
How would I get the code from this website in a string variable that I can parse?

How to download a tar file received in HTTP Response using javascript?

I am getting a tar file in HTTP response body. I need to download the content as a tar file for the user. How can it be done in javascript?
There's nothing special about the scenario. Simply navigate to the URL.
window.location = 'http://example.com/your-tar-file.tar';
create a html link with the download attribute ,trigger the click attribute for that link
<a class="tar-generate" href="path/to/generated/tar" download=download>Download tar</a>
js for triggering :
$('.tar-generate').click();//trigger('click');
if this doesn't work you need to save that generated file to your server using file_put_content or any other file saving function, and then generate a download link
Make http request by passing the url, and give a callback function. In the Callback function write your logic
Example code:
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
//callback Function
httpGetAsync('https://api.github.com/users/abc', function (response) {
window.location = response.<tarLink> // here give the tar link
})

Submitting code snippets to database using XMLHttpRequest

In my website, I send data to a database using XMLHttpRequest. I tried submitting code snippets and simple Spanish language characters, but when I check the received data, I only see what comes after those code snippets or characters, like this:
Data sent:
Hello <?xml version="1.0" encoding="utf-8"?>
Data received:
Hello
So, is there any way to submit code snippets or special characters using XMLHttpRequest? If there isn't, which is the best way besides php?
Here is my XMLHttpRequest code:
//call the function for the XMLHttpRequest instance
//create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
var xmlHttp;
if(window.XMLHttpRequest) {//for Forefox, IE7+, Opera, Safari, ...
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject) {//for Internet Explorer 5 or 6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//create pairs index=value with data that must be sent to server
var data = "title="+title+"&desc="+desc;
//sets the request
xmlHttp.open("POST","http://mywebsite.com/database.php",true);
//adds a header to tell the PHP script to recognize the data as is sent via POST
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//sends the request
xmlHttp.send(data);
//Check request status
//If the response is received completely, will be transferred to the HTML tag with tagID
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
document.getElementById("div").innerHTML=xmlHttp_refresh_es_ES.responseText;
}
}
Always encode your data before sending it, that way spaces and special characters shouldn't be an issue (as long as it's UFT8)
var data = "title=" + encodeURIComponent(title) + "&desc=" + encodeURIComponent(desc);

XMLHttpRequest only works in IE

I am using the XMLHttpRequest object for my AJAX calls which has been working fine across browsers with a callback handler I have created to return JSON based on the request type and arguments etc.
But I am now integrating an external RESTful API to return a JSON string and for some reason it only works for me in IE (tested in IE 8). Using fiddler 2 I have determined that the API is returning the correct data.
I get the XMLHttpRequest.readyState of 4 but XMLHttpRequest.status only returns 0 for Chrome, Safari and FF. I read that sometimes when using a local server (test server) you always get a status of zero so I bypassed my check for status but still got a blank string for XMLHttpRequest.responseText.
function ajaxRequest(requestType,url) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
switch (requestType)
{
case 5:
//Home postcode search
showAddresses("home", xmlhttp.responseText);
break;
}
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
You should upgrade to jQuery as it will handle this request for nearly all browsers. But if you really want to know how to use XMLHttpRequest across all browsers, here's some code that seems to do the trick: https://github.com/ilinsky/xmlhttprequest/blob/master/XMLHttpRequest.js
Or, just pick apart jQuery's implementation.
http://api.jquery.com/category/ajax/
Hope this helps.
My issue was that I was using an external API and xmlHttpRequest only allows you to makes calls on the same server. I moved my data call into my server code and got the response from my callback handling page instead of going straight out to the API.

Categories