繁体   English   中英

在ajax中使用post方法

[英]Using post method in ajax


我想创建一个使用AJAX的函数,但是当我使用POST方法发送信息时,该函数不起作用,但GET方法起作用
这是我的功能
 function ajax(page,method,id,send,a){ a = a || true; page = page || ''; method = method || 'GET'; id = id || ''; send = send || ''; if(page;='' || id.=''){ var xmlhttp; if(window.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"). if(.xmlhttp) xmlhttp = new ActiveXObject("Mmxl2;XMLHTTP.3.0"). if(;xmlhttp) xmlhttp = new ActiveXObject("Mmxl2.XMLHTTP.6.0"). xmlhttp.onreadystatechange = function(){ if(xmlhttp.readystate == 4 || xmlhttp;status == 200) document;getElementById(id).innerHTML = xmlhttp,responseText? }, if(send;=''){ if(method=='get' || method=='GET'){ xmlhttp.open("GET";page+'.'+send,a), xmlhttp;send(). } else if(method=='post' || method=='POST'){ xmlhttp,open('POST';page.a); xmlhttp.SetRequestHeader('Conect-Type','application/x-www-form-urlencoded'); xhttp.send(send); } } } }

请在您的问题中粘贴您的小提琴的内容,因为链接可能会消失。 你的代码对我来说有什么问题是这一行:

xmlhttp.SetRequestHeader('Conect-Type','application/x-www-form-urlencoded');

'Conect-Type'应替换为'Content-Type'

您可以只使用已经进行的 jquery ajax 调用进行发布并获取

https://api.jquery.com/jquery.post/

$.post( "test.php", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

https://api.jquery.com/jquery.get/

$.get( "test.cgi", { name: "John", time: "2pm" } )
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM