AJAX Jquery url not working - php

I have a strange problem with Jquery Ajax with the following code.
Situation 1:
function leuk(decrease_id, user_id) {
$.ajax({
type: "POST",
url: 'http://schoolprove.nl/nieuw/index.php/leerlingen/checkvoortgang/',
data: 'decrease_id=' + decrease_id + '&user_id=' + user_id,
success: function (msg) {
$('#output').html(msg);
}
});
}
Situation 2
function leuk(decrease_id, user_id) {
$.ajax({
type: "POST",
url: '/nieuw/index.php/leerlingen/checkvoortgang/',
data: 'decrease_id=' + decrease_id + '&user_id=' + user_id,
success: function (msg) {
$('#output').html(msg);
}
});
}
The AJAX url works sometimes whith http:// and sometimes without. I build and error catch when a error occured. This works very well at IE but Firefox doesn't give a error. So at some computers with Firefox this will not work. It is very strange and i don't know why it will not work.
Situation 1: Works sometimes
Situation 2: Works sometimes
Sometimes situation 1 works and ad a other computer situation 2 works, WHY?
Anybody know how to solve?
Thank you very much!!

Redirect your domain like this tonerize.com to www.tonerize.com will solve this issue
please read this http://en.wikipedia.org/wiki/Same_origin_policy. for more information

Is your ajax file located on some other server?
If no then, you need not specify the the whole path for url.
Its enough if you use
url: 'ajaxfilename.php' //depending on the folder the file is located

I found the solutions it is causing due to cache, try to disable the cache and add the random value in your query string.
function leuk(decrease_id,user_id)
{
$.ajax({
type: "POST",
cache:fale,
timeout:10000,
url: '/nieuw/index.php/leerlingen/checkvoortgang/?rnd='+Math.random(),
data: 'decrease_id='+decrease_id+'&user_id='+user_id,
success: function(msg){
$('#output').html(msg);
}
});
}

You can not use a path such as this for AJAX.
http://something.com/file.php
It has to be relative to your file and located on your server like to.
/file.php
I am not quite sure what your problem is though, your question does not provide any error information.

Related

Use AJAX/JSON to pass an array of objects to PHP

I need to pass a Javascript array of objects to PHP. Found about 3 solutions on the internet, but none of them worked for me. The array looks like this (in Chrome dev console):
And my current AJAX call looks like this:
function consolidate() {
var studentsstring = JSON.stringify(students);
$.ajax({
type: "POST",
url: "writefile.php",
dataType: "json",
data: {students: students},
success: function(data) {
alert(data);
}
});
}
And the PHP file looks like this:
<?php
print($_POST['students']);
?>
Currently, when I hit the "consolidate" button, nothing happens. No alert is shown. Chrome dev reports no errors in the code.
Oh, I'm dumb. Too many hours of coding. The line should have been:
data: {students: studentsstring},
and now it works. Thanks all.

change content of a remote file using ajax

I'm trying to change content of chatt.php file on server, using ajax procedure.
$("#btnsend").click(function(){
var a = $("#write").val();
console.log(a); // that's ok
$.ajax({
type: "POST",
url: "ajax.php",
data: a,
dataType: "json",
success: function () {
alert("323");
}
});
});
ajax.php
$a = $_POST["a"];
$b = "chapters/chatt.php");
file_put_contents($b, $a);
There is no alert, chatt.php is not changed, console is empty.
Any help?
First of all add php error_reporting().
Error Reporting Manual
In php you are getting the Undefined index notice for $_POST['a']. You need to pass a from ajax as:
Modified code:
$("#btnsend").click(function(){
var a = $("#write").val();
console.log(a); // that's ok
$.ajax({
type: "POST",
url: "ajax.php",
data: "a="+a,
dataType: "json",
success: function () {
alert("323");
} });
});
As my other mate mentioned in comments if you solve this issue you will face an another issue like Parse error for this line:
$b = "chapters/chatt.php"); // unexpected bracket
Side note:
Keep in mind error_reporting() is only for development not for productions.
You aren't sending correct data, try:
data: {a: a}
You never bothered naming your data parameter, so $_POST['a'] doesn't exist. PHP expects key=value for POST data, and you're sending over a bare value.
Try
data: { a: a}
^--key
^---value
instead.
And note that you're opening your server up to a total remote compromise. If this chatt.php is inside your site's document root, a malicious user can use your code to write ANY php code they want to the file, and your server will happily execute it for them.

net::ERR_EMPTY_RESPONSE when post with ajax

Hello im trying to upload a xlsx file one by one so i can show a status bar, the problem is, i did it with a for loop and while loop sending a request via ajax, but when is on the 40th element it stops and the console shows POST (site.php) net::ERR_EMPTY_RESPONSE, i tried to do it in the localhost and it works perfect, but when a i try to do it on my external server(godaddy) it shows the error. here is the code.
for(j=1;j<=tama;j++){
$.ajax({
type: "POST",
url: "ejphp.php",
dataType: "json",
data: {vals: regs, j:j},
success: function(datos){
console.log(j)
prog=datos['progreso_r'];
var id_vac=datos['id_vac']
var tipo=datos['tipo']
var tipo2=datos['tipo2']
var tot_ing=datos['tot_ing']
prog_p=Math.round(prog*100/tama);
$("#progressbar").val(prog_p);
$("#progreso").text(prog_p+'%');
$("#datos_vac").text('Id Vacuno: '+id_vac);
if(prog_p==100){
$("#aceptar").show("slow");
}
if(tipo=='error') registro(tipo2, id_vac)
subir(parseInt(prog)+1);
return false;
}
})
}
Maybe your server can't pass more than 40 requests, try to increase php memory limit and execution time. And try to browse in godaddy's admin panel.

jQuery POST not working - seems it should be

It's been a while since I've played with jQuery, so I'm wondering what I'm missing here.
I've set up a php page that writes a test txt file when the jQuery calls it.
The txt file is being written, so jQuery is finding the php page, but then displays my error alert, not my success one, so something is not working.
My php:
$testfile=$_SERVER["DOCUMENT_ROOT"].'/test.txt';
$file5 = fopen ($testfile, "w");
fwrite($file5, 'testy');
fclose ($file5);
exit();
My jQuery:
var g = {
r: 1
};
$.ajax({
type: "POST",
url: "/test.php",
cache: false,
data: g,
success: function (data) {
alert('yay');
},
error: function (data) {
alert('nay');
}
});
Anything obvious I'm missing?
Thanks for taking a look.
I think the problem is on the url you are requesting. Its hard to tell without know the type of error or without knowing your folder structure or if you are using any PHP framework. Please, write details
Try this--
url: "test.php",

Opencart multisite registration strange request

I am having a problem that is really stumping me. In my Opencart installation my account registration page isn't working. It is a multisite and the page works fine on the other site. The problem is with the zones, when it tries to get zones based on the country it throws an error. When I examined it with Firebug I see that it sends an OPTIONS request instead of a GET request like it does when generating a successful request on the other page. Unfortunately this isn't the only problem, I was able to get it to send a GET request by specifying "crossDomain: 'false'" as an argument in the .ajax call and that still doesn't fix the error. The cookies being sent and returned are also different. I'm trying to find the underlying problem or at least something that will fix the issue.
Edit:
I added responded to questions in a comment as it Stackoverflow did allow me any more links.
2nd Edit:
I've found that the way you access the registration page matters. Some links to it don't generate any problems. I'm thinking more and more that this probably has something to do with cookies.
The OPTIONS request is indeed very strange in this case and almost looks like the client side (JS/browser) do not know what request methods could be used on the server.
Anyway, in the template catalog/view/theme/<YOUR_THEME>/template/account/register.tpl find this piece of code (almost end of file):
<script type="text/javascript">
$('select[name=\'country_id\']').bind('change', function() {
$.ajax({
url: 'index.php?route=account/register/country&country_id=' + this.value,
dataType: 'json',
beforeSend: function() {
and before dataType: 'json', add type: 'get', or type: 'post', so You should end up with:
<script type="text/javascript">
$('select[name=\'country_id\']').bind('change', function() {
$.ajax({
url: 'index.php?route=account/register/country&country_id=' + this.value,
type: 'post',
dataType: 'json',
beforeSend: function() {
By this You specify which concrete HTTP request method should be used.

Categories