问题
I am trying to execute an entire PHP file via the onClick event of some link.
<a onClick="phpSubmit();" class="submitButton">Submit</a>
Makes a successful call to this jQuery function
function phpSubmit()
{
alert('submitting');
$.post("/submit.php");
alert('submitted');
}
Which fails to execute the PHP file "submit.php", in the same directory.
Any ideas?
I have tried $.ajax, $.get and "submit.php" as well as "/submit.php". I don't know why this isn't working, repost $.ajax stuff if you think I was doing something wrong.
EDIT:
It's always the little things, I mistyped a character when I imported the jQuery script which is why $.ajax was undefined.
The above method WORKS.
回答1:
Rather than use $.post I prefer, and find it much easier for debugging, to use something like:
$.ajax({
dataType: 'html',
type: 'POST',
url: 'submit.php',
cache: false,
data: 'foo=bar',
error: function(e){
alert(e.status);
},
success: function(response){
alert(response);
}
});
I would also suggest removing the /
in front of submit.php
- no need if it's in the same directory.
回答2:
You can get the error message if fail to post the data to PHP file. use $.ajax();
error : function(XMLHttpRequest,textStatus,ErrorThrown)
{
alert(textStatus);
}
回答3:
use $.post("submit.php");
without a slash
来源:https://stackoverflow.com/questions/13608850/calling-php-file-from-jquery-not-working