I was making a automation script to extract some info from a website, And It's important to submit some info using POST method. Can anyone tell me how to use HTTP Post method with Imacro & javascript for firefox plugin. Below is the script which i found here : Sending an HTTP Post using Javascript triggered event
But it's giving me error when i play the same using Imacro player.
var url = "http://www.google.com/";
var method = "POST";
var postData = "Some data";
var async = true;
var request = new XMLHttpRequest();
request.onload = function () {
var status = request.status; // HTTP response status, e.g., 200 for "200 OK"
var data = request.responseText; // Returned data, e.g., an HTML document.
}
request.open(method, url, async);
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(postData);
XMLHttpRequest() is no longer supported in firefox 15+
You have to define it:
const XMLHttpRequest = Components.Constructor("#mozilla.org/xmlextras/xmlhttprequest;1");
var request = XMLHttpRequest();
To run JavaScript in iMacros you can use this method.
URL GOTO=javascript:window.ScrollTo(0,150);
Try this method.
In your case it would look like this.
URL GOTO=javascript:var url = "http://www.google.com/";var method = "POST";var postData = "Some data";var async = true;var request = new XMLHttpRequest();request.onload = function () var status = request.status; var data = request.responseText; request.open(method, url, async);request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");request.send(postData);
Related
When I put var in front of method and url (var method, and var url) the code does not work.
function intervalcheck(theObject) {
var xmlhttp = new XMLHttpRequest(),
method = 'POST',
url = 'https://thekmui.com/payb/payresult/result.php';
xmlhttp.open(method, url, true);
xmlhttp.onload = function () {
Take a look at here:
var xmlhttp = new XMLHttpRequest(),
method = 'POST',
You are putting ','(comma) after each line. Either you leave it empty and browser handle the rest. Or you can put ';' semi-colon like this:
var xmlhttp = new XMLHttpRequest();
method = 'POST';
Otherwise the code will produce a massive error.
You may follow this post as well:
Send POST data using XMLHttpRequest
I've been trying to get info from JIRA REST API to put it in cells inside a spreadsheet. For this I'm writing a Google script attached to the spreadsheet, so, form what I understand, that makes this "in Browser" stuff.
In my project I currently have a main code script as well as the requirejs script (I read that it would be the way for me to call xmlhttprequest from a browser script).
Please keep in mind I'm not that used to scripting and mostly tried to follow documentations, but I must be missing something because I always get this error:
TypeError: Cannot find function open in object function localRequire(deps, callback, errback) {...}.
Here's my function so far:
function updateFilters()
{
var Test = destinationSpreadsheet.setActiveSheet(destinationSpreadsheet.getSheetByName("Nonsense"));
var url = "myurl is in there"; //I have a test url in there
var XMLHttpRequest = require(["xmlhttprequest"], function(xmlhttprequest){});
var myRequest = new XMLHttpRequest();
myRequest.open('GET', url);
myRequest.onload = function ()
{
console.Log(myRequest.response);
};
}
I'm confused as to why "open" doesn't work. There seem to be no issue when I comment out the line, but of course if I do that I get nothing.
Try this for javascript:
function updateFilters()
{
var url = "myurl is in there"; //I have a test url in there
var myRequest = new XMLHttpRequest();
myRequest.open('GET', url);
myRequest.onload = function ()
{
console.Log(myRequest.response);
};
}
This line is breaking your code:
var XMLHttpRequest = require(["xmlhttprequest"], function(xmlhttprequest){});
Try this for node.js:
function updateFilters()
{
var url = "myurl is in there"; //I have a test url in there
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var myRequest = new XMLHttpRequest();
myRequest.open('GET', url);
myRequest.onload = function ()
{
console.Log(myRequest.response);
};
}
Note: you new to install xmlhttprequest with npm or yarn first
EDIT: sorry, had the code ridden with errors. changed it now
Ive been trying to extract some JSON data from a github link to put it in a html file, but everytime I try to do so (with the code below), I get an Uncaught TypeError: cannot read property "value" of null. This even though "value" (in the Json file) certainly is not null.
What is going wrong here?
Thanks
you can check out the json file i put on github https://github.com/BearsAreFriendly/PolitiekeHelderheid/blob/master/kamerleden.json . I am quite certain that "value" has been defined
var section = document.querySelector("section");
var requestURL = 'https://github.com/BearsAreFriendly/PolitiekeHelderheid/blob/master/kamerleden.json';
var request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onload = function(){
var phdata = request.response;
showKamerleden(phdata);
}
function showKamerleden(jsonObj) {
var kamerleden = jsonObj["value"];
for (var i = 0; i < kamerleden.length; i++) {
var myPara1 = document.createElement('p');
myPara1.textContent = kamerleden[i].Id;
section.appendChild(myPara1);
}
}
this should print out the id's. instead I get my mentioned error code
I believe you may not actually be receiving any JSON data from GitHub, but HTML. When my browser requests a GitHub page the response content type is Content-Type: text/html. Your request URL should respond with JSON in order for it to be usable. When you request the GitHub URL you are likely getting other stuff (such as HTML), but not JSON.
For example, this url serves JSON data only: https://my-json-server.typicode.com/typicode/demo/posts
It has Content-Type: application/json when I inspect the page in dev tools under the network section
Modifying your code to add the url below works perfectly.
var section = document.querySelector("section");
var requestURL = 'https://my-json-server.typicode.com/typicode/demo/posts';
var request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onload = function(){
var phdata = request.response;
/* showKamerleden(phdata); */
document.write(JSON.stringify(phdata) )
}
try this
var kamerledens = jsonObj.value;
instead of
var kamerledens = jsonObj["value"];
and also you should use a loop to go through all ids
function showKamerleden(jsonObj) {
var kamerledens = jsonObj.value;
for (var i = 0; i < kamerledens.length; i++) {
var myPara1 = document.createElement('p');
myPara1.textContent = kamerledens[i].Id;
section.appendChild(myPara1);
}
}
Edited:
Try moving
request.send();
after the onload function
It isn't "value" that it is null, jsonObj is null.
Hence the error 'cannot read property "value" OF null'
I suggest you output and analyse the request in the callback
console.log(request)
I have a text file on my server and I want to upload a text in it using XMLHttpRequest. It is downloaded successfully via GET method, but when I try to POST it I get 404 error.
var r1 = new XMLHttpRequest();
r1.open("GET", "db.txt", false);
r1.send();
var str = r1.responseText + "foo text";
var r2 = new XMLHttpRequest();
r2.open("POST", "db.txt", false);
r2.send(str);
Doing a direct upload as you're trying would be a security concern in most places and is generally not permitted... What you need to do is have a middle layer on your server to handle the request and write the file to disk safely.
You can easily upload a file using something like this:
var jsonBlob = new Blob([someJSON], {type: "text/plain;charset=utf-8"});
var data = new FormData();
data.append("filename", "new.json");
data.append("json", jsonBlob);
var xhr = new XMLHttpRequest();
var postURL = "http://example.com/post_target";
xhr.open("POST", postURL, true);
xhr.onload = function(e) {
if (this.status == 200) {
console.log(e.target.response);
}
};
xhr.send(data);
Where postURL is an endpoint which can handle file uploads.
If you post what language(s) you have available on your server (PHP?) I can give some example code to handle the upload on the server's end.
I have a simple script that does a cross site request and gets data from a GitHub gist. The data from the Github API is returned as a JSON string. To allow further modification of the data, I want it as a JSON object.
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
var tmpJSON = "";
var gistData = "";
var gistID = "5789756";
var gitAPI = "https://api.github.com/gists/"
var gistQuery = gitAPI + gistID;
function incrementGist() {
gistData = createCORSRequest('GET', gistQuery);
gistData.send();
tmpJSON = JSON.parse(gistData.response);
}
In the html page, I have
<p><input type="button" value="Increment" OnClick="incrementGist()"></p>
If I actually hit the button, the error I get is:
Uncaught SyntaxError: Unexpected end of input
But if I subsequently open the console and run this:
var crap = JSON.parse(gistData.response);
it works just fine. This happens in both Firefox and Chrome. I really don't see why the JSON.parse command fails inside a function call, but not in the console. An actual page is set up here
The problem is that you're trying to read the response before the server answered.
You must read the response in a callback. For example :
gistData = createCORSRequest('GET', gistQuery);
gistData.onreadystatechange = function() {
if (gistData.readyState === 4) {
if (gistData.status === 200) {
tmpJSON = JSON.parse(gistData.response);
... use tmpJSON...
... which should not be called so as it is not JSON...
... maybe tmpObject ?
}
}
}
gistData.send();
That's because you are not waiting the request to actually finish. I don't know your API but try waiting the server response then parse your JSON. you could try with a SetTimeout first to see that it is working but you nee to do something like in jQuery with its' success:function(...) callback