jQuery post/ajax post problem
HI im trying to send a jquery post/ajax to send some variables to a php file.
<script src="/jquery.js"></script>开发者_Python百科
<script type="text/javascript">
var music ='';
var genre ='';
var language ='';
var nasheedid = "<? echo $postid; ?>";
var session = "<? echo $_SESSION['SESS_TMP']; ?>";
$(function(){
$('.submit').click(function(){
var postdata = {pmusic: music, pgenre: genre, plang: language, psession: session};
$.ajax({
type: 'post',
cache: false,
url: 'addmusic.php',
data: postdata,
success: function (msg) {
alert(msg);
},
dataType: "text",
});
});
});
now regardless of where i post the stuff to, (even if i change the url to url: blahblah.php123 I still get the same results (which is):
The alert displays the entire source code of the file (that the above code is on)
May be you are having an exception on those pages and its redirecting to the same page so you are seeing the same page everytime. Try to debug your server side code and see.
thanks guys i fixed the problem myself. My server was using rewrite so i had to use url: "/addmusic.php"
instead of url: "addmusic.php"
.
So you see the PHP code in the alert? There are a few causes for this.
- You don't have a WAMP stack installed. If not, you need to install WampServer or XAMPP.
- It's not configured properly. It could be that PHP isn't setup correctly.
- Your configuration doesn't support short tags (
<?
). Try changing them to<?php
. - You're not accessing the file through the server properly. Look at the beginning of the URL, make sure it starts with http://localhost/ .
You might also try using jQuery.post()
instead.
It's also best to use json_encode()
when outputting from PHP to Javascript.
var nasheedid = <?php echo json_encode($postid); ?>;
var session = <?php echo json_encode($_SESSION['SESS_TMP']); ?>;
It will take care of quoting, escaping, etc.
精彩评论