javascript - 在ajax中使用post方法

标签 javascript php html ajax


我想使用 AJAX 创建一个函数,但是当我使用 POST 方法发送信息时,该函数不起作用,但 GET 起作用方法有效
这是我的function :

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);
    }
    }
    }
} 

最佳答案

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

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

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

关于javascript - 在ajax中使用post方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45935116/

相关文章:

php - 查询同步mysql

javascript - 改变jquery对话框的颜色

javascript - 从本地磁盘加载图像

php - DataTables 警告 : JSON data from server could not be parsed. 这是由 JSON 格式错误引起的

javascript - 代码作为 Opera UserJS 运行,但在 Chrome 和 Greasemonkey 用户脚本中给出 "undefined"错误

php - mysql错误:Unknown column 'variable' in 'field list'

javascript - 如何在树节点 CSS 上显示全宽背景色

html - 图像拆分为表 : Remove space between rows

javascript - 使用 naught 以零停机时间部署 node.js

php - MySQL VARCHAR 字段或 PHP substr