简体   繁体   English

Ajax - 参数没有传递 - 没有jQuery

[英]Ajax - parameters not passing - no jQuery

Unfortunately, I'm getting nothing but jQuery results it seems. 不幸的是,我似乎只得到jQuery结果。 I'm looking for the correct way to pass parameters via AJAX, old browser fallbacks are not necessary, but no libraries, please. 我正在寻找通过AJAX传递参数的正确方法,不需要旧的浏览器回退,但请不要使用库。 If there's another thread I've missed on this, please link =) 如果还有另一个我错过的帖子,请链接=)

I'm using $, but it's a custom object/whatever, not jQuery. 我正在使用$,但它是一个自定义对象/什么,而不是jQuery。

$.ajax({
    't':'POST',
    'u':e, 
    'd':{ajax:'1'},
    's':function(data){
        console.log(data.response);
        document.getElementById('mainc').innerHTML = data.response;
    },
    'e':function(data){
        console.log(data);
    }
});

Which calls: 哪个电话:

$.ajax = function(a){
if(!a.u){return false;};
a.t=a.t||"GET";
typeof a.a=='undefined'?true:a.a;
a.e=a.e||function(){return false;};
a.s=a.s||function(){return true;};
var x=new XMLHttpRequest();
x.open(a.t,a.u,a.a);
x.onreadystatechange=function(){
    if(x.readyState===4){
        if(x.status===200){
            a.s(x);
        }else{
            a.e(x);
        }
    }
};
a.t==="post" ? x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") : null;
x.send(a.d);

} }

x.send(ad) should pass the {ajax:'1'} . x.send(ad)应该传递{ajax:'1'} I've tried {'ajax':'1'} and just 'ajax=1' as well. 我试过{'ajax':'1'}并且只是'ajax=1' Not sure why NONE of the parameters I try to pass are making it server side. 不知道为什么我尝试传递的参数没有任何东西使它成为服务器端。 I'm very certain the parameters are not hitting the server, although the request seems to otherwise send and receive without issue. 我非常确定这些参数没有命中服务器,尽管请求似乎在没有问题的情况下发送和接收。

Try setting request header to 尝试将请求标头设置为

x.setRequestHeader("Content-Type", "application/json")

Check this out. 看看这个

XMLHttpRequest.send() doesn't accept javascript / JSON objects if you set x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") . 如果设置x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") XMLHttpRequest.send()不接受javascript / JSON对象。

You should format to urlencoded variables as mentioned here: 您应格式化为urlencoded变量,如下所述:

Query-string encoding of a Javascript Object Javascript对象的查询字符串编码

or use x.setRequestHeader("Content-Type", "application/json") like another answer suggested. 或者使用x.setRequestHeader("Content-Type", "application/json")就像建议的另一个答案一样。

Also: 也:

at==="post" should be at==="POST" since it won't change the request header because of case-sensitive checking. at==="post"应该at==="POST"因为它不会因为区分大小写的检查而更改请求标头。

Full working code (with urlencoded data): 完整的工作代码(使用urlencoded数据):

$ = {}

$.ajax = function(a){
if(!a.u){return false;};
a.t=a.t||"GET";
typeof a.a=='undefined'?true:a.a;
a.e=a.e||function(){return false;};
a.s=a.s||function(){return true;};
var x=new XMLHttpRequest();
x.open(a.t,a.u,a.a);
x.onreadystatechange=function(){
    if(x.readyState===4){
        if(x.status===200){
            a.s(x);
        }else{
            a.e(x);
        }
    }
};
a.t==="POST" ? x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") : null;
x.send(a.d);

}    

$.ajax({
    't':'POST',
    'u':'/', 
    'd':"ajax=1",
    's':function(data){
        console.log(data.response);
        document.getElementById('mainc').innerHTML = data.response;
    },
    'e':function(data){
        console.log(data);
    }
});

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

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