Ajax post is working in localhost but not in godaddy server - ajax

This ajax post is working in localhost but not in godaddy server. I dont know how to solve this kind of issues. Please let me know how rectify this issue.
Ajax is not working in many places. Locally all of files are working very fine.
I am new to php. Anyone can help me to fix this bug?
Thanks in Advance
function advanced_addTopic(cid) {
$.ajax({
url: "assets/php/checkSubtopicStatus.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: {'cid':cid}, // Data sent to server, a set of key/value pairs (i.e. form fields
success: function(data) // A function to be called if request succeeds
{
if(data==="True"){
$("#subtopicDiv").html($("#subtopicDiv"+cid).html());
$("#advanced_testid").val(cid);
var hiddenCourse=$("#createTest").attr('class');
$("#courseHidden").val(hiddenCourse);
$("#advanced_addquestionModal").modal('show');
$("#subtopic").focus();
$("#question").focus();
var tempVal=$("#getID").text();
$("#advanced_courseHidden").val(cid);
} else {
alert("Create subtopics to insert questions!");
}
}
});
My PHP CODE IS:
<?php
class loginValidation {
function validate()
{
ob_start();
session_start();
include "../../connection.php";
$id=$_POST['cid'];
$query = mysql_query("select * from advanced_subtopic where testid='$id'");
if(mysql_num_rows($query)>0){
echo "True";
} else {
echo "False";
}
}}$loginValidation=new loginValidation;
$loginValidation->validate();
?>

Using Ajax URLs like
url: "http://domainname.com/assets/php/checkSubtopicStatus.php";
If you use your domain name HTTPS enter the same as an Ajax URL.

Related

Cakephp 2.0 Ajax post data not showing in the controller?

I'm working cakephp web application, I have created a small Ajax function in default ctp file. setup file now I want to send value data to another controller function, in the chrome browser network showing that Ajax posting to desire url but in controller post value didn't show up when I try to echo $_post ['selectedlocation']??
Ajax Default File
var DisplayLocationName = $('#ListingLocation option:selected').val();
$.ajax({
type: "POST",
url: '/listings/home',
data: {selectedlocation:DisplayLocationName },
dataType:"text",
success: function(result) {
}
});
Listing Controller Function
function home()
{
if(!isset($this->params['requested']) || $this->params['requested'] != true)
{
$this->actionId = 'home';
}
$this->viewFile = null;
if($this->params['isAjax'] && isset($_POST['selectedlocation']))
{
echo "erreerreer";
echo $selectloc= $_POST['selectedlocation'];
$this->set('selectloc',$selectloc);
}
}
how do I display Ajax post value in default for testing purposes that, make sure me that Ajax is posting proper values to the controller
You will need to use the logging files to read the post data. This will write the data to the error log and you can view it there.
function home()
{
if(!isset($this->params['requested']) || $this->params['requested'] != true)
{
$this->actionId = 'home';
}
$this->viewFile = null;
if($this->params['isAjax'] && isset($_POST['selectedlocation']))
{
echo "erreerreer";
CakeLog::write('error', $this->request->data('selectedlocation));
echo $selectloc= $_POST['selectedlocation'];
$this->set('selectloc',$selectloc);
}
}
When you want to $_POST a data from an ajax request, you should use:
$form_data = $this->request->data; (cakephp 2.x)
$form_data = $this->params['form'] (cakephp 1.3)
Now you got an array with your $_POSTed data, try to var_dump() it to understand its sctructure if you want.

Wordpress: Use AJAX to get the next post

After looking through the jQuery documentation and many stackexchange community forums, I am still faced with this problem. Taking little bits from here and there have helped me get this far, but I am stuck where I am now.
Im using an ajax request to try and load the next post after the one that is currently displayed. The only issue I run into is when I try to execute the method included in my php file:
<?php
echo getnext();
function getnext(){
$post = get_post($_POST['id']);
$prevPost = get_previous_post();
return $prevPost->post_content;
}
?>
I can echo the POST variable that is being passed in fine, but once I try to actually call the method I get a 500 internal Server Error.
My AJAX request looks like this:
setTimeout(function (){
$currid = $('#post_id').val();
$.post("wp-content/themes/stargazer/populate.php",
{
"id":$currid
},
function(data){
//$("#academiccontent").html(data);
alert (data);
});
$('#academiccontent').animate({ 'opacity': 1 });
}, 1000);
Any help would be greatly appreciated, Ive been stuck on this for a long while now.
Thanks!!
Why don't you use AJAX directly in WordPress?
The best way is add to function.php file in your theme something like this:
add_action( 'wp_ajax_getnext', 'getnext' );
function getnext() {
$post = get_post($_POST['id']);
$prevPost = get_previous_post();
return $prevPost->post_content;
die(); // this is required to return a proper result
}
And your javascript change to this:
setTimeout(function (){
$currid = $('#post_id').val();
var data = {
"action": "getnext",
"id":$currid
};
$.post(ajaxurl, data,
function(data){
alert (data);
});
$('#academiccontent').animate({ 'opacity': 1 });
}, 1000);
More info about AJAX in WordPress you can find here: http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

jQuery AJAX request error status 0

I've been developing an application locally and am running into trouble on the actual server.
Whenever I submit an AJAX request with jQuery it gives me an error with error status:0 and and statusText: 'error'.
The Chrome inspector doesn't even show a response code for the request, it just says failed.
When I inspect it closer, I notice that all of the data was sent and the PHP file actually processed it. For example, in the request below, the user was indeed created. The bad response code is preventing other requests from executing (since they depend on a 'successful' response).
Here is a sample request:
var cct = $.cookie('ttc_csrf_cookie'); // csrf protection
var sUrl = "<?= base_url(); ?>user/create/";
var serialized = {
name: me.name,
email: me.email,
oauth_provider: 'facebook',
oauth_uid: me.id,
ttc_csrf_token: cct
};
$.ajax({
url: sUrl,
type: "POST",
data: serialized,
error: function(someStuffHere) {
//* THIS CODE RUNS. SHOWS ERROR STATUS: 0 */
},
success: function(user_id) {
//******** THIS HERE NEVER RUNS ***********///
}
});
And here is the corresponding PHP code:
public function create() {
if($this->input->post()) {
$user['name'] = $this->input->post('name');
$user['email'] = $this->input->post('email');
$user['oauth_provider'] = $this->input->post('oauth_provider');
$user['oauth_uid'] = $this->input->post('oauth_uid');
$user['last_activity'] = date('Y-m-d H:i:s');
$user_id = $this->Users->create_user($user);
$this->session->set_userdata('user_id', $user_id);
echo $user_id;
}
}
These two snippets are only an example. All AJAX requests won't work on the live sever.
What could it possibly be? Thanks!
UPDATE: I've narrowed it down and the issue occurs when echo'ing a result. When I comment out the echo, no error is thrown (but, of course, no result is sent).
Turns out that the issue was caused by the compress_output option in CodeIgniter. When it's set to true, the echo's don't work. Thanks for everyone that tried to help!

Why does Ajax never return a value even with callbacks

Ajax never returns a value. I have tried setting the async:false option and also tried to setup a callback function it still never returns a value. When i browse to the url using firefox i see the expected response but when i make the request via ajax, there is no reponse. Firebug also confirms it.
I have tried lots of code samples i found but they never return a value. I have also tried using a different version jquery and other browsers.
Does anyone know what could be wrong?
Thanks
Below is the code that gets called when a user clicks a button on the form.
function login() {
var username = $("#uname").val();
var password = $("#password").val();
$.ajax({
type: 'POST',
url: 'http://localhost/mConnect/login.php',
data: { username: username, password: password },
async: false,
success: function(html) {
slim(html);
}
});
}
function slim(html) {
// var data = $(xml).find("Status").text();
alert(html.responseText);
}
Below is the login.php it just prints static xml
<?php
$array = array('stat' => '1.0',
'mode' => 'whatever',
'content' => 'All');
$new ='<?xml version="1.0" encoding="iso-8859-1"?><response>';
foreach($array as $key => $values) {
$new .= "<$key>$values</$key>";
}
echo $new.'</response>';
?>
You have a success callback, but no failure callback. Probably something along the way is failing and there are about 100 possibilities. Run it in Firebug (or equivalent) and see what happens to the request. (My money: your local webserver isn't responding at all.)
If you sent an AJAX request you will get a response. Even if it is a timeout. So if you don't have a response at all then you more than likely never sent the request.

.ajax posts and gets response on local server, no response on web host

I'm using an ajax call to do a minor calculation then return the value and display it in the page same page where the form is submitted. In Firebug it says it calls the function, however doesn't get a response. (I have a similar form that writes to a database that works fine, seemingly because it doesn't need a response - firebug says it fails to get a response on that script as well.) The odd thing is that I wrote this on my local server before implementing it on the site and everything worked as planned. I'm using Code Igniter on both the local server and the web server, but I don't know if that has something to do with it. Anyways, any help would be great. I'm marginally new so this is kinda outta my realm at this moment.
Thanks
EDIT: .js
$(document).ready(function() {
$('#submit').click(function(){
var formdata = {
years: $('#years').val(),
rate: $('#rate').val(),
principle: $('#principle').val(),
periods: $('#periods').val(),
continuous: $('#continuous').val()
}
$.ajax({
url: "http://localhost:8888/CodeIgniter_1.7.2/index.php/timevalueshow/submit",
type: 'POST',
data: formdata,
success: function(data){
$('#replace').replaceWith('<p>'+data+'</p>');
}
});
return false;
});
});
php submit function
function submit(){
$years = $this->input->post('years');
$rate = $this->input->post('rate');
$principle = $this->input->post('principle');
$periods = $this->input->post('periods');
$isCont = $this->input->post('continuous');
$params = array(
'years' => $years,
'rate' => $rate,
'principle' => $principle,
'periods' => $periods,
'isCont' => $isCont
);
$this->load->library('timevalue', $params);
return $this->timevalue->FVFactor();
}
Could it be that the request is being made cross-domain? Remember that mydomain.com is considered a different domain to www.mydomain.com.
I ran into a similar situation recently. I requested a page from mydomain.com which made an AJAX request to a script on www.mydomain.com. The request was not made because it was considered cross-domain. It had the same symptoms that you describe. In Firebug and Chrome Developer Console I saw an empty response and no error message.
The problem is that CodeIgniter generates absolute URLs based on the $config['base_url'] setting. If you access the site using a different domain name to what is configured in $config['base_url'] you can run into this type of problem.
This works on the dev and not on the server because you are calling localhost!
// this will have the client call itself on this particular page (wont work)
url: "http://localhost:8888/CodeIgniter_1.7.2/index.php/timevalueshow/submit",
The above code should be just:
// this is relative to the document ROOT, will work on server but not on dev!
// you can set it relative to the calling page using ../ as needed
url: "/index.php/timevalueshow/submit",

Resources