Login ProductsSalesSupportDownloadsAbout |
Home » Technical Support » Elevate Web Builder Technical Support » Support Forums » Elevate Web Builder General » View Thread |
Messages 11 to 20 of 31 total |
Stuck At Getting JSON Values From PHP Script |
Fri, Jul 14 2017 9:46 AM | Permanent Link |
Tim Young [Elevate Software] Elevate Software, Inc. timyoung@elevatesoft.com | Frederick,
<< My PHP script has the following code:- header('Content-type: application/json'); echo json_encode($rows); In Chrome, under the console tab, I see: Resource interpreted as Document but transferred with MIME type application/json: "http://localhost/gsta17/getgstaw.php?domain=tsw.carat.my". >> This means that, although you're sending back JSON and Chrome *knows* that you *said* you were sending back JSON, it thinks that what is coming back is *HTML* (or possibly XML), not JSON. In general, the problem is that the response isn't formatted correctly as JSON. You can see the actual response data in the developer tools in Chrome (F12/Network), and that should provide you with the information that you need to fix this. If you want to post your entire PHP script, I can certainly try it here and give you a solution. << and in EWB's internal server, I issue the ShowMessage(Request.ResponseContentType) and get: >> I think you mean in your EWB application. If you're accessing a PHP script, you're not using the EWB Web Server, you're using Apache, etc. Tim Young Elevate Software www.elevatesoft.com |
Fri, Jul 14 2017 9:53 AM | Permanent Link |
Tim Young [Elevate Software] Elevate Software, Inc. timyoung@elevatesoft.com | Frederick,
<< After spending a bit of time checking, I have found out that the internal EWB server does not support the "Content-type: application/json" header sent by the PHP script. The internal server keeps returning "Content-type: application/octet-stream". >> That's not even remotely correct. The internal EWB Web Server isn't even involved in what you're doing, and when the EWB application reports the content-type header, it's just showing you what the web server/web server application (your PHP script) returned. << When I copied the EWB application to a web site using Apache server and ran it, the Request.ResponseContentType returned the intended content type and Request.ResponseContent returned the correct JSON data. >> Okay, now I think I get what you're trying to do. Are you trying to load the EWB application using the internal web server and have it make cross-origin requests to *another* web server that is hosting your PHP application scripts ? If so, then you need to make sure that the *other* web server is set up properly to handle cross-origin requests: https://stackoverflow.com/questions/29150384/how-to-allow-cross-domain-request-in-apache2 << Either that, or just always deploy/load the EWB application from the same web server (IOW, don't use the internal EWB Web Server). You can set up applications to auto-deploy during a run in the IDE (Project/Options/Deployment): https://www.elevatesoft.com/manual?action=viewtopic&id=ewb2&topic=Deploying_Project and you can set up an external web server and select it as the web server to run against in the IDE: https://www.elevatesoft.com/manual?action=viewtopic&id=ewb2&topic=Running_Project Tim Young Elevate Software www.elevatesoft.com |
Fri, Jul 14 2017 12:10 PM | Permanent Link |
Frederick Chin | "Matthew Jones" wrote:
/* Can you be a bit more precise in this please? I don't understand why your PHP is doing anything with the EWB server at all. EWB doesn't run PHP, and PHP would presume a different server. I'm confused. */ If I run the application using the internal server and there is no external server running, checking the Request.ResponseContentType will always return "application/octet-stream and Request.ResponseContent.Text will return the entire PHP script. I don't know if EWB is running PHP or not because it didn't barf when it was called to perform a server request on a PHP script. If it was not supposed to run a PHP script, it should have thrown an exception and not report an unexpected Response content. If the application is run from an external server, the application reports the correct and expected Response content. That is why I was confused. /* And the "fails here" is not clear. What is the exact error? In the F12 Chrome debugging, you can trap the error and see the contents of the various variables. I suspect that it hasn't been created if it is null. */ The error message where the line fails is "Unable to get property 'tdbadata_dtsipadd' of undefined or null reference". From the message, I was not able to determine what went wrong but I later found out that it was because the TDatabase was not created yet. I miss the debugger. -- Frederick |
Fri, Jul 14 2017 12:24 PM | Permanent Link |
Frederick Chin | Tim,
/* This means that, although you're sending back JSON and Chrome *knows* that you *said* you were sending back JSON, it thinks that what is coming back is *HTML* (or possibly XML), not JSON. In general, the problem is that the response isn't formatted correctly as JSON. You can see the actual response data in the developer tools in Chrome (F12/Network), and that should provide you with the information that you need to fix this. If you want to post your entire PHP script, I can certainly try it here and give you a solution. */ I have searched for the reason and it seems that this is normal and does not affect the JSON data. You can make Chrome not show the message by using "text/html" for the content type. As for the JSON data, I can assure you that it is in the correct format that EWB requires because I checked in like, more than 10 times. I am beginning to dream braces, brackets and quotation marks! /* I think you mean in your EWB application. If you're accessing a PHP script, you're not using the EWB Web Server, you're using Apache, etc. */ The thing is I thought the EWB web server is calling the PHP script because it does not display an error and it is simply returning unexpected Response content. Another reason why I think this way is when I am running the application using the internal server, Apache is not running at all. -- Frederick |
Fri, Jul 14 2017 12:34 PM | Permanent Link |
Frederick Chin | Tim,
/* Okay, now I think I get what you're trying to do. Are you trying to load the EWB application using the internal web server and have it make cross-origin requests to *another* web server that is hosting your PHP application scripts ? If so, then you need to make sure that the *other* web server is set up properly to handle cross-origin requests: */ The PHP file is in the Output directory. I think there is no cross-origin calls involved because the URL for the PHP file is just the PHP file name and Apache is not running. I am using the internal web server. /* << Either that, or just always deploy/load the EWB application from the same web server (IOW, don't use the internal EWB Web Server). You can set up applications to auto-deploy during a run in the IDE (Project/Options/Deployment): */ I did this for the reply below. /* and you can set up an external web server and select it as the web server to run against in the IDE: */ This solved the problems I had. I created an external web server entry and ran the application with it selected. After fixing the TDatabase not being created, I was able to load the JSON data provided by the PHP script into the dataset. There are now two questions left I would like to ask. 1. I have a grid with the dataset property entered. If the property is filled before I load the rows, the grid does not show any records. If I fill the dataset property after I load the rows, the records show up. Is this expected behaviour? 2. My MYSQL table has a date field with the entry as '2017-12-31'. When the dataset is loaded, the date field in the dataset changes to 1/1/1970. How do I retain the date from MYSQL? -- Frederick |
Fri, Jul 14 2017 12:37 PM | Permanent Link |
Raul Team Elevate | On 7/14/2017 12:24 PM, Frederick Chin wrote:
> The thing is I thought the EWB web server is calling the PHP script because it does not display an error and it is simply returning unexpected Response content. Another reason why I think this way is when I am running the application using the internal server, Apache is not running at all. No - EWB web server itself cannot execute PHP script (since it does not have PHP support nor php runtime) so all it can do it returns its content as normal file(same way it returns content of HTML, JS, PDF, etc files). If you want to use PHP then you have to host PHP from some other web server (i.e. apache) that is PHP enabled. There are few options here: 1. You can host the whole EWB application from this other web server (in this case EWB Web Server would not be used at all). This would be easiest. 2. You can host the EWB app from EWB Web server but then in your EWB app retrieve all the data from your other web server - this require your other server allowing cross origin queries (Tim posted info on this in this thread) 3. You could create a module that run on EWB web server and basically queries other server - this is most complex For you option 1 above is one you likely want Raul |
Fri, Jul 14 2017 12:46 PM | Permanent Link |
Frederick Chin | Big Al wrote:
/* My PHP script is goofing up the data a little bit at the top and bottom of the JSON, and I'm not sure how to fix the php code, so I just strip it out here. If you figure out how to fix the php code so the extra brace is not put in there please let me know so I can not have to do the replace statements. */ The portion of the PHP code that I use to get the MYSQL data is as follows:- $sql = "select * from mytable"; $result = $conn->query($sql); if (!$result ) { die("The table data could not be retrieved: " . $conn->connect_error); } $rows = array(); while($r = $result->fetch_assoc()) { $rows[] = $r; } header('Content-type: application/json'); echo json_encode(array('rows'=>$rows)); In the OnComplete event of the the TServerRequest call, my code is procedure TfrmMain.RequestComplete(Request: TServerRequest); var strWork: String; begin HideProgress; if (Request.StatusCode=HTTP_OK) then begin strWork := Request.ResponseContent.Text; //strWork := strReplace(strWork,'[[{','[{'); //strWork := strReplace(strWork,'}]]','}]'); dbaData.dtsIpadd.Open; dbaData.dtsIpadd.LoadRows(strWork); dbaData.dtsIpadd.Open; dbaData.dtsIpadd.LoadRows(strWork); // Don't forget to create an AfterLoad event call end else ShowMessage('Response Error: '+Request.StatusText); end; There is no need to perform StrReplace() calls on the returned JSON data. -- Frederick |
Fri, Jul 14 2017 12:56 PM | Permanent Link |
Frederick Chin | Raul,
/* No - EWB web server itself cannot execute PHP script (since it does not have PHP support nor php runtime) so all it can do it returns its content as normal file(same way it returns content of HTML, JS, PDF, etc files). */ Now I know! Maybe EWB can be enhanced to check if it supports a script. If not, throw up an appropriate message and stop running. /* 1. You can host the whole EWB application from this other web server (in this case EWB Web Server would not be used at all). This would be easiest. */ I did the following for the best results for internal testing of PHP scripts:- 1. Create an external web server with the URL pointing to localhost/<sub-directory> 2. Deploy the output directory to Apache's HTDOCS <sub-directory> on run 3. Run the application using the external Apache server -- Frederick |
Fri, Jul 14 2017 9:14 PM | Permanent Link |
Raul Team Elevate | On 7/14/2017 12:56 PM, Frederick Chin wrote:
> > Now I know! Maybe EWB can be enhanced to check if it supports a script. If not, throw up an appropriate message and stop running. > This part of EWB is really just a generic file serving web server so it cannot just throw up an error. How would it know when to do so (and even what error to throw up) ? There are tons of scripting languages and PHP is just one sample - and maybe i DO want to serve PHP file content if my site has coding samples or such. This is really something that comes down to system setup - only place files in EWB published folders you actually want it to serve up. Raul |
Sat, Jul 15 2017 11:37 AM | Permanent Link |
Frederick Chin | Raul wrote:
/* This part of EWB is really just a generic file serving web server so it cannot just throw up an error. How would it know when to do so (and even what error to throw up) ? There are tons of scripting languages and PHP is just one sample - and maybe i DO want to serve PHP file content if my site has coding samples or such. This is really something that comes down to system setup - only place files in EWB published folders you actually want it to serve up. */ Previously, I was mentioning the problem while running the application in the internal server. Then, I realised today, the problem could also happen with EWB's web server. This turned out to be true. Currently, my HTML, JS and PHP files are in the content folder served by EWB's web server. The PHP file is referencing data from my local MYSQL. As advised and confirmed, the TServerRequest returns 'application/octet-stream' for the Request.ResponseContentType and the entire PHP file for the Request.ResponseContent.Text properties because EWB's web server does not handle PHP files.. If I move the PHP file to a remote web site with the URL of 'tsw.carat.my/getgstaw.php' with an expected parameter of 'domain=tsw.carat.my', what should I enter in TServerRequest.URL? procedure TfrmMain.Button2Click(Sender: TObject); begin ShowProgress('Getting Data'); MyRequest.URL:='????' MyRequest.Params.Add('domain=tsw.carat.my'); MyRequest.OnComplete:=RequestComplete; MyRequest.Execute; end; I tried 'tsw.carat.my/getgstaw.php' and the response error is 'not found'. If I enter 'http://www.tsw.carat.my/getgstaw.php' or 'http://tsw.carat.my/getgstaw.php', the response error is a blank. Note: I test entered 'tsw.carat.my/getgstaw.php?domain=tsw.carat.my' in Chrome and I received a JSON string, as expected. -- Frederick |
« Previous Page | Page 2 of 4 | Next Page » |
Jump to Page: 1 2 3 4 |
This web page was last updated on Tuesday, April 30, 2024 at 03:55 PM | Privacy PolicySite Map © 2024 Elevate Software, Inc. All Rights Reserved Questions or comments ? E-mail us at info@elevatesoft.com |