javascript xmlhttp.open("GET", url, true)

Use this board to ask questions or have discussions with other Rich Displays users.
Post Reply
cknopp
New User
Posts: 2
Joined: Mon Aug 04, 2014 11:44 am
First Name: Chris
Last Name: Knopp
Company Name: Rochester Drug Cooperative
Phone: (585)271-7220 e5206
Address 1: 50 Jetview Drive
City: Rochester
State / Province: New York
Zip / Postal Code: 14624
Country: United States
Contact:

javascript xmlhttp.open("GET", url, true)

Post by cknopp »

I have created a service program that is called using PHP. The service program is an SQLRPGLE that returns a result set. This allows me to created a java script function for an OnChange event. I have created it using XML and JSON. When the XML and JSON is stored in a folder "/profoundui/userdata/xml/cust.xml" I get a result set returned. The issue I am having is when I build a URL interactively I do not get a connection. I have a field called "Cust" and on called "Item" that is used when building the URL. When the URL is put a browser I get a result. I looked at your AJAX api's but we can't figure out how to use them.

we would like to use a URL like -

var Item = document.getElementById('Item').value;
var Cust = document.getElementById('Cust').value;
var ItmQty = 1;

var url = "http://rdcportal/getcost.php?item=" + Item + "&cust=" + Cust + "&qty=" + ItmQty;

Below are the JavaScripts -
-----------------------------------------------------------------------------------------------------------------------------------
// OnChange -

function getPrice(){

var xmlhttp = new XMLHttpRequest();
var url = "/profoundui/userdata/xml/pcr0080.txt";

xmlhttp.onreadystatechange = function() {

if ((this.readyState == 4 && this.status == 200)) {

var xmr = JSON.parse(this.responseText);

document.getElementById("ItemCost").innerHTML = xmr.$COST;
document.getElementById("Method").innerHTML = xmr["@COST_MTHD"];

var c = JSON.stringify(xmr.$COST);
alert("c = " + c);
changeElementValue( "Cost", c );

var m = JSON.stringify(xmr["@COST_MTHD"]);
alert("m = " + m);
changeElementValue( "Meth", m );

}

};

xmlhttp.open("GET", url, true);
xmlhttp.send();
}

getPrice();

-----------------------------------------------------------------------------------------------------------------------------------
--- OR ---

// OnChange -

function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "/profoundui/userdata/xml/cust.xml", true);
xhttp.send();
}

function myFunction(xml) {

var xmlDoc = xml.responseXML;

x = xmlDoc.getElementsByTagName("cost")[0];
y = x.childNodes[0];
c = y.nodeValue;
changeElementValue( "Cost", c );

x = xmlDoc.getElementsByTagName("cost_mthd")[0];
y = x.childNodes[0];
m = y.nodeValue;
changeElementValue( "Meth", m );

}

loadDoc();


-----------------------------------------
FYI this works - but is not useful

var Item = document.getElementById('Item').value;
var Cust = document.getElementById('Cust').value;
var ItmQty = 1;

var url = "http://rdcportal/getcost.php?item=" + Item + "&cust=" + Cust + "&qty=" + ItmQty;

postToNewWindow(url);
Attachments
Capture.JPG
Capture.JPG (84.32 KiB) Viewed 1228 times
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: javascript xmlhttp.open("GET", url, true)

Post by Scott Klement »

So if I understand correctly... your PHP script works when using postToNewWindow, or when saving it's results to a file. It does not work when calling the PHP via the browser's built-in XMLHttpRequest. But, XMLHttpRequest works fine when retrieving a static file.

Hard to say why that would be from the information provided. I would recommend that you troubleshoot the code and find out where it is going wrong. All you've told us here is that you "do not get a connection." It's very unclear what this means... Are you saying that you get an error like "connection refused"? It makes absolutely no sense that you'd get that if it works with postToNewWindow, since it's the same URL... but if that is what you're getting, it means your web server is not running.

More likely, you ARE getting a connection, but something is going wrong. This is where you need to look deeper... what is going wrong?

Use the web browser's developer tools, check out the network requests, see what is happening. If the server is giving you an error, look in the HTTP server log, look in the PHP diagnostic stuff (I don't remember how to do this, it's been about 7 years since I used PHP.. I don't like PHP very much... but I do remember there was a way to look at the errors, I just don't remember the specifics.)

None of what you've posted seems to have anything to do with Profound UI. (XMLHttpRequest is a feature of the web browser. PHP is a programming language, usually provided by Zend/RogueWave, not Profound Logic.)

Also, I do not think these are related to your immediate problem, but here are some things that will make your application more robust:

1) Assuming you are using Profound UI Rich Displays, you should use the pui.get() and pui.set/changeElementValue APIs to retrieve/set values rather than directly manipulating the browser's HTML DOM. There's more to Profound UI than just the HTML tags, so when you access the HTML directly, you are bypassing our code, which might cause problems down the road.

Code: Select all

var Item = pui.get('Item');
var Cust = pui.get('Cust');
(You are already using changeElementValue for setting new values -- which is fine.)

2) you should always encode values that you put into a URL (or any type of URI). Certain characters are not valid in a URI, and others have a special meaning... so make sure that they are encoded properly.

Code: Select all

var url = "http://rdcportal/getcost.php?item=" + encodeURIComponent(Item) + "&cust=" + encodeURIComponent(Cust) + "&qty=" + encodeURIComponent(ItmQty);
3) Not sure that it's a good idea to hard-code "http://rdcportal" in the URL. If you ever switch domain names or machines, etc, you'd have to change the application. Instead, just leave off the http://hostname part, and it'll automatically go to the same host that your page is running on.

Code: Select all

var url = "/getcost.php?item=" + encodeURIComponent(Item) + "&cust=" + encodeURIComponent(Cust) + "&qty=" + encodeURIComponent(ItmQty);
cknopp
New User
Posts: 2
Joined: Mon Aug 04, 2014 11:44 am
First Name: Chris
Last Name: Knopp
Company Name: Rochester Drug Cooperative
Phone: (585)271-7220 e5206
Address 1: 50 Jetview Drive
City: Rochester
State / Province: New York
Zip / Postal Code: 14624
Country: United States
Contact:

Re: javascript xmlhttp.open("GET", url, true)

Post by cknopp »

Thanks Scott. I meant by not connecting that readyState=4 and status=0. While in debug I found it is a CORS error. "(Reason: CORS header ‘Access-Control-Allow-Origin’ missing)". We are running the PHP on a ZEND server.
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: javascript xmlhttp.open("GET", url, true)

Post by Scott Klement »

Ah, so you are running PHP on a completely separate instance of the HTTP server (or maybe even on a separate machine entirely.) And, when you were getting the data as a file, you were not retrieving it from the PHP server, you were retrieving it from the Profound UI server, and that's why that worked.

So there are several alternative solutions:

1) Run both PHP and Profound UI in the same HTTP server instance. This is what most of our PHP customers do, it really makes it easy. (But, of course, you may have a good reason for running a separate instance?)

2) Add the Access-Control-Allow-Origin header, as explained in the error message you found. If you're writing your own PHP code, this might be almost as easy as #1. (But, very difficult when using any 3rd party PHP applications, since you pretty much have to modify the 3rd party code.)

3) Use a proxy, so that certain URLs hitting your Profound UI server are proxied over to your PHP server. I wouldn't bother with this method if the PHP is running on the same box, but if it's running elsewhere and you don't want to modify the programs to add the ACAO header, then this is a good choice.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest