I have some script that works on my dev server but not on my staging server:
add_action('wp_head','get_gz_info',30);
function get_gz_info(){
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var modal = {action:'modal_action'};
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
$.post(ajaxurl,modal,function(data){
$('body').append(data);
});
});
</script>
<?php
}
the php is roughly:
add_action('wp_ajax_modal_action', 'set_modal');
add_action('wp_ajax_nopriv_modal_action', 'set_modal');
function set_modal() {
...
}
Everything works fine on my dev side but the staging side the javascript is placed in the header (just like the dev) but it won't run the "ajax part". Could it be that the staging side requires a username/password to access it?
I've attempted it with and without the https and get the same results.
According to the inspect this is being set as the ajaxurl, "...mysite.../wp-admin/admin-ajax.php" so the ajaxurl is being implemented. The odd issue is it works as is on one server but not the next.
----- EDIT -----
The html shows this as the js in the head (after jquery loads)
<script type="text/javascript" >
jQuery(document).ready(function($) {
var modal = {action:'modal_action'};
var ajaxurl = '...mysite.../wp-admin/admin-ajax.php';
$.post(ajaxurl,modal,function(data){
$('body').append(data);
});
});
</script>
The person who setup the staging server placed and .htaccess in the admin side that prevented the site from accessing files in the wp-admin area.
Related
I have a very basic webpage which contains of two files, phpcode.php.cgi and frontpage.php.cgi.
I have dropdowns that should be populated dynamically, however I can't get the request/reponse to work. The ID of the dropdown is "start".
I have an Ajax request in the frontpage file:
$(document).ready(function () {
$("#start").change(function(){
alert("dropdown changed");
var val = $('#start').val();
$.ajax({
type: "GET",
data: {Station_Nr : val},
url: "phpcode.php.cgi",
success: function(data){
alert(data);
}
});
});
});
and my phpfile looks like this:
<?php
if(isset($_GET['Station_Nr'])) {
echo "it works";
};
?>
It tries to request from url../phpcode.php.cgi?Station_Nr=27
When I try to enter the page I get a 500 error.
According to chrome, the error lies in the row which starts with $.ajax.
I have included:
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
Thanks!
In the top of the file is #!/usr/bin/php5 located.
Here was \r\n added because of Windows and the transfer method was binary. The Apache server responded with an error and when replacing \r\n with \n it works.
When you obtain a 500 error is because something is wrong in server not in client, use /phpcode.php.cgi in $.ajax to use an absolute path just in case.
I am trying to load the output of a PHP script into a using JavaScript and JQuery. The JavaScript function I am using uses the $.get function in JQuery to call a php script, which I want to display in another division. The script I wrote is:
<script type="text/javascript">
function on_load() {
$(document).ready(function() {
alert('here');
$.get("http://localhost/dbtest.php", function(data){
alert('here too too');
$("uname").html(data);
});
});
}
</script>
The PHP script (dbtest.php) uses a simple echo statement:
echo "hello, world!!!";
I am getting the first alert here, but not the second. What Can I be doing wrong here?
I suppose uname is a ID, in that case you should use:
$("#uname").html(data);
You can add this to your php for debugging:
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);
Try also to remove http:// from your ajax call and use relative path instead.
The guys below pointed out that the selector is wrong. Indeed that's a problem, but I think that the real issue is that you don't get the second alert. Probably your php file localhost/dbtest.php is not accessible. What happen if you open localhost/dbtest.php in a new tab?
I think the problem is the path to your dbtest.php file. You are saying you seecond alert will not be displayed so your request must be wrong.
Try to copy your page into the same folder like dbtest.php open the page in your browser with http://localhost/yourfile.php
If this does not display both alert-boxes try the same with an open developer console (Chrome/IE = F12) and look if there are errors.
You say you are trying to make ajax requests to your localhost from a Phonegap application. Phonegap prevents making ajax requests to other domains by default. You must add localhost to whitelist. Here is more detail: http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html
Here you have a working example:
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<div id="uname"></div>
<script>
function on_load() {
$(document).ready(function() {
alert('here');
$.get("http://localhost/dbtest.php", function(data){
alert('here too too');
$("#uname").html(data);
});
});
}
on_load();
</script>
Beware of the same-origin-policy. The page loading dbtest.php must be from the same origin, unless you grant other origins by adding a header from dbtest.php.
Try add some error handling to your code to better see what´s happening.
<script type="text/javascript">
function on_load() {
$(document).ready(function() {
alert('here');
$.get("http://localhost/dbtest.php", function(data){
alert('here too too');
$("uname").html(data);
}).fail(function () {
alert("failed");
});
});
}
</script>
I have been using pusher for quite a few months with success. I won't go into the details of the "push" part of the solution because that works already. My issue is with the listener side when I try to listen to a second app. Please note that I said a second app not a second channel on the same app.
Here is what I have that has worked well for at least 6 months and continues to work well until I try to add a second version of this on the same HTML/PHP page in the HEAD section.
I have changed the keys an certain info for obvious reasons.
How can I add a second copy of this pointing to a second app within Pusher?
My concern is that I will have issues if there are things like identical variables such as channel. I have tried renaming the channel to channel 2 and pusher to pusher 2 but then it quits working..
<!-- Start Pusher Code -->
<script src="https://d3dy5gmtp8yhk7.cloudfront.net/2.1/pusher.min.js" type="text/javascript"></script>
<script type="text/javascript">
var pusher = new Pusher('0000000000');
var channel = pusher.subscribe('appname');
channel.bind('channelname', function(data) {
MyURL = 'http://www.google.com';
newwindow=window.open(MyURL,data.cuid,'height=800,width=950,scrollbars=yes');
});
</script>
<!-- End Pusher Code -->
You can use a self-executing closure:
( function() {
// some code
} )();
to ensure that the variables don't clash. Also, remember to declare variables using var or they leak to the global scope.
If possible, I would include the Pusher <script> tag just once.
<script src="https://d3dy5gmtp8yhk7.cloudfront.net/2.1/pusher.min.js"></script>
<!-- Start Pusher Code -->
<script>
( function() {
var pusher = new Pusher('app_key_1');
var channel = pusher.subscribe('appname');
channel.bind('channelname', function(data) {
var MyURL = 'http://www.google.com';
var newwindow=window.open(MyURL,data.cuid,'height=800,width=950,scrollbars=yes');
});
} )();
</script>
<!-- End Pusher Code -->
<!-- Start Pusher Code -->
<script>
( function() {
var pusher = new Pusher('app_key_2');
var channel = pusher.subscribe('appname');
channel.bind('channelname', function(data) {
var MyURL = 'http://www.google.com';
var newwindow = window.open(MyURL,data.cuid,'height=800,width=950,scrollbars=yes');
});
} )();
</script>
<!-- End Pusher Code -->
I've used two different application keys to confirm that you are connecting to two different applications.
im having a problem with getting any response from my server using Jquery and Ajax.
Server side:
$data = strtotime("now");
echo $data; // $data
Client side:
html code...
<script type="text/javascript" src=".../modjpicker2.js"></script>
</body>
</html>
modjpicker2.js:
$(function() {
$.ajax({
type: "GET",
url: "ajaxtime",
data: "{}",
success: function(response) {
var currentTime = new Date();
}
});
});
And var currentTime is just not being created... Some how request doesn't go through...
Forgot to note that jQuery is working fine all scripts are attached in header and php is working good as well, link ajaxtime is live also.
In your html
<script type="text/javascript" src=".../modjpicker2.js"></script>
It should be
<script type="text/javascript" src="../modjpicker2.js"></script>
I had kind of the same problem: My servers responses won't get handled by jQuery or JavaScript. It seems that nothing were transmitted.
I solved the issue by enabling HTTP access control on my server( More here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)
Here's my solution:
jQuery client won't accept my server responses
Hope I can help! Let me know if it worked
I have got this code
/* Popup for hot news */
$(document).ready(function() {
var $dialog = $('<div></div>')
.html('text to be shown')
.dialog({
autoOpen: false,
title: 'Table'
});
This code is in my JavaScript files which are included by header.php.
How to pass PHP output to this function?
Inputing <?php echo($mydata)?> in .html('') above does not solve anything.
Any reason why this gives an error?
Thanks for helping!
First of all your code is having some problem, $ in php is a prefix of variable and $ in jQuery is a selection sign. You can view the html source(the output) on browser for debug.
I can think of three ways to do so:
(1)The way you have mentioned should work, see the code below
<?php $foo='hi'?>
<script>
alert("<?php echo $foo?>");
</script>
(2)Another way is to separate server side and client slide code clearly for better maintenance
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<?php echo"<div id='foo' style='visibility:hidden;'>HI</div>"?>
<script>
alert($('#foo').text());
</script>
(3)The last way is by passing variable from the URL http://example.com/example.php#hi
<script>
var foo= location.href.split('#')[1];
alert(foo);
</script>
Adding <?php //code?> to your JS files do nothing because JS files are not executed by Apache. You can either use Ajax to request for data once your page loads (if the data doesn't need to be there at start) or you can add those PHP code blocks to your HTML code.
Using Ajax (goes in your JS file)
$(document).ready(function(){
var u = 'data.php';
var d = {};//data
$.get(u, d, function(data){
//got by data. lets invoke some methods here
});
});
Putting it in your HTML (goes in your HTML file)
var __DATA = '<?php //output some data I prepared earlier ?>';
//I'm using __ and capitol case to denote a global variable. Just personal preference
<script src='myjsfile.js' type='text/javascript'></script>
//the variable __DATA is available to your JS file and you can use it