Shaun
Shaun

Reputation: 2181

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.

Upvotes: 0

Views: 39

Answers (1)

Fazal Rasel
Fazal Rasel

Reputation: 4526

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",

Upvotes: 1

Related Questions