0

Unfortunately, I'm getting nothing but jQuery results it seems. I'm looking for the correct way to pass parameters via AJAX, old browser fallbacks are not necessary, but no libraries, please. If there's another thread I've missed on this, please link =)

I'm using $, but it's a custom object/whatever, not 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(a.d) should pass the {ajax:'1'}. I've tried {'ajax':'1'} and just 'ajax=1' as well. 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.

Randy Hall
  • 7,716
  • 16
  • 73
  • 151
  • The fact that your code is obfuscated makes it a pain to try to understand what it means. If you want help, make your code readable. No one will be able to maintain that code except perhaps you, if you work with it. Every day. Forever. – PatrikAkerstrand Oct 22 '12 at 21:38

2 Answers2

1

Try setting request header to

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

Check this out.

Community
  • 1
  • 1
Sunny
  • 4,765
  • 5
  • 37
  • 72
1

XMLHttpRequest.send() doesn't accept javascript / JSON objects if you set x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded").

You should format to urlencoded variables as mentioned here:

Query-string encoding of a Javascript Object

or use x.setRequestHeader("Content-Type", "application/json") like another answer suggested.

Also:

a.t==="post" should be a.t==="POST" since it won't change the request header because of case-sensitive checking.

Full working code (with urlencoded data):

$ = {}

$.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);
    }
});
Community
  • 1
  • 1
Khôi
  • 2,133
  • 11
  • 10