Hello I am building a widget. The app has to be run locally on the device but needs php. I have a web server that has the php files and when the whole widget is just put on the server it works fine but when its locally run I cant't call the php files using ajax. How do I call serverside php from locally hosted javascript
Asked
Active
Viewed 34 times
-1
-
Why can't you call the PHP files using AJAX? What prevents you from doing this? – David Mar 05 '17 at 18:06
-
It doesnt go through when its called locally but it does when the js is on the server and not locally hosted – johnnyjw Mar 05 '17 at 18:07
-
1Cross origin scripting @David - I guess locally hosted means `file://` protocol – mplungjan Mar 05 '17 at 18:07
-
Do you host your files in a local web server (like xampp, etc...) or do you just open up the HTML file with the browser? – Larce Mar 05 '17 at 18:07
-
@mplungjan: I would think so as well, but the OP should really be specifying what the problem is. We can guess at solutions to possible problems, but the first step in actually solving the problem would be to know what the actual problem is. – David Mar 05 '17 at 18:09
-
@Larce The php files are hosted on godaddy but the html/JS are just ran locally in the browser – johnnyjw Mar 05 '17 at 18:10
-
@mplungjan Cross origin scripting does not prevent AJAX requests to other domains / servers. – Larce Mar 05 '17 at 18:11
-
Locally as in `file:////somedir/page.html` will prevent Ajax requests – mplungjan Mar 05 '17 at 18:11
-
@user6850954: Define "ran locally"? Are you just opening the files from the file system and not from any kind of web server? In that case you're not just crossing origin, you're crossing protocol. You should *probably* be opening web pages from a web server. Browsers have a variety of security restrictions in place on JavaScript code for a variety of good reasons. One of those restrictions prevents file-system-based code from making network requests. – David Mar 05 '17 at 18:12
-
@David The project is to build an iWidget which is just a widget for iOS. So iOS will just be opening the .html from the local iOS file system. I need that to access php scripts from my website – johnnyjw Mar 05 '17 at 18:15
1 Answers
1
When you just open your HTML files locally, AJAX requests will be prevented. See here: Ajax in Jquery does not work from local file
You can spin up a local web server and then it should work, but there may be a better solution. What exactly are you trying to archieve? Should it be more like an application?
-
The project is to build an iWidget which is just a widget for iOS. So iOS will just be opening the .html from the local iOS file system. I need that to access php scripts from my website – johnnyjw Mar 05 '17 at 18:17
-
Thank You I looked at your post and it was just chrome that was the prblem – johnnyjw Mar 05 '17 at 18:37