[英]Using ajax to post a form to php without refresh
I have this written up for send a few variables to a php script: 我已经写了一些用于向php脚本发送一些变量的代码:
$(function() {
$('a[class="removeUnread"]').click(function(){
var markedtopicid = $(this).attr("id");
var sessionvar = \'', $context['session_var'], '\';
var sessionid = \'', $context['session_id'], '\';
$.ajax({
url: "index.php?action=quickmod;board=', $context['current_board'], '",
type: "POST",
data: sessionvar + "=" + sessionid + "&topics[]=" + markedtopicid + "&qaction=markread",
});
});
}); });
I think this is the correct way to send post data via ajax, but it doesn't appear to be sending. 我认为这是通过ajax发送帖子数据的正确方法,但似乎没有发送出去。 Was I right to wrap the code in the ready function?
我将代码包装在ready函数中对吗?
I can tell you right off the bat you should not have a semicolon between quickmod and board in your URL. 我可以立即告诉您,您的URL中不应在quickmod和board之间使用分号。 I'm answering here because i cannot post comments yet.
我在这里回答,因为我还不能发表评论。 One good tool to use in web development ESPECIALLY with GET and POST requests is googles PostMan app.
谷歌PostMan应用程序是一种特别适用于GET和POST请求的Web开发工具。 Its free to use and what it does is it will show you the exact output of any link you send it.
它免费使用,它的作用是向您显示您发送的任何链接的确切输出。 So you can try putting the link that you make via javascript into postman and see what errors it spits out.
因此,您可以尝试将通过javascript制作的链接放入邮递员,看看它吐出了什么错误。
In this example i'm pretty sure your URL is all kinds of screwed up though. 在此示例中,我非常确定您的URL各种各样。 Try this instead...
试试这个...
"index.php?action=quickmod&?board="+$context['current_board']
fyi, i did not test that link so it may not work. 仅供参考,我没有测试该链接,所以它可能无法正常工作。 If it doesnt work, google some ajax examples and javascript string concatenation.
如果不起作用,请使用Google的一些Ajax示例和javascript字符串连接。 You're string is not suitable for ajax.
您的字符串不适合Ajax。
is should be like this... 应该是这样的...
$.ajax({
url :'index.php',
type : 'POST',
data : { sessionvar: sessionid, topics:markedtopicid},
success : function (data) {
},
error : function () {
}
Try handling the error: 尝试处理错误:
$.ajax({
url: "index.php?action=quickmod;board=', $context['current_board'], '",
type: "POST",
data: sessionvar + "=" + sessionid + "&topics[]=" + markedtopicid + "&qaction=markread",
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.responseText);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.