Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (turingtest):

Performing POST with a JSON object. Is it me, or is it them? I have to POST the following JSON object: application { Boolean isTest (required) String firstName (required) String lastName (required) String email (required) String phone (required) String zipcode (required) String describeYourself (required) ool[] objectLanguages (required) education[] education (required) experience[] experience (required) certification[] certs }

OpenStudy (turingtest):

education { String school (required) Integer graduationYear (required) String degree (required) String major (required) } experience { String company (required) Date fromDate [yyyy-MM-dd] (required) Date toDate [yyyy-MM-dd] (required) String title (required) String workDone (required) } certification { String certification (required) Date dateCertified [yyyy-MM-dd] (required) } ool { String language (required) [must include at least 'javascript'; include any other OOP languages you know] Integer proficiency (required) [scale of 0-10, 0 being none, 10 being proficient] }

OpenStudy (turingtest):

I cam up with an object that looks like so: ``` { "application" : { "isTest" : true, "firstName" : "Max", "lastName" : "Spiegel", "email" : "xxx@gmail.com", "phone" : "xxxx", "zipcode" : "xxx", "describeYourself" : "xxx", "objectLanguages" : [ {"language" : "python", "proficiency" : 10}, {"language" : "javascript", "proficiency" : 10}, {"language" : "php", "proficiency" : 10}, {"language" : "ruby", "proficiency" : 10} ], "education" : [ {"school" : "xxx", "graduationYear" : 2065, "degree" : "PhD", "major" : "computer science"}, ], "experience" : [ {"company":"xxx", "fromDate":start1, "toDate":end1, "title":"xxx", "workDone":"xxx"}, ], "certs" : [ {"certification" : "xxx", "dateCertified" : cert1}, ] } } ```

OpenStudy (turingtest):

I made the Ajax request with the following code: ``` function ajaxRequest(){ var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken) for (var i=0; i<activexmodes.length; i++){ try{ return new ActiveXObject(activexmodes[i]) } catch(e){ //suppress error } } } else if (window.XMLHttpRequest) // if Mozilla, Safari etc return new XMLHttpRequest() else return false } var mypostrequest=new ajaxRequest() mypostrequest.onreadystatechange=function(){ if (mypostrequest.readyState==4){ if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){ document.getElementById("result").innerHTML=mypostrequest.responseText } else{ alert("An error has occured making the request") } } } mypostrequest.open("POST", " https://mkpartners.secure.force.com/services/apexrest/careers?firstName=Max&lastName=Spiegel&email=turingtestable @gmail.com", true); mypostrequest.setRequestHeader("Content-type", "application/json; charset=UTF-8"); mypostrequest.send(//JSON object here) ``` but I keep getting back a 400 error. When i check the object 'mypostrequest' in the console i see that I am get the following ``` XMLHttpRequest {statusText: "Bad Request", status: 400, response: "[{"message":"Unknown field: careers_RestWebService…1, column:592]","errorCode":"JSON_PARSER_ERROR"}]", responseType: "", responseXML: null…} onabort: null onerror: null onload: null onloadend: null onloadstart: null onprogress: null onreadystatechange: function (){ ontimeout: null readyState: 4 response: "[{"message":"Unknown field: careers_RestWebService.oolWrapper.language at [line:1, column:592]","errorCode":"JSON_PARSER_ERROR"}]" responseText: "[{"message":"Unknown field: careers_RestWebService.oolWrapper.language at [line:1, column:592]","errorCode":"JSON_PARSER_ERROR"}]" responseType: "" responseXML: null status: 400 statusText: "Bad Request" timeout: 0 upload: XMLHttpRequestUpload withCredentials: false __proto__: XMLHttpRequest ``` the [{"message":"Unknown field: careers_RestWebService.oolWrapper.language at [line:1, column:592]" message seems to mean that their servers code for checking my request is missing the "language" field, so I tried to remove it. If i do so, the code then runs until it encounters ther "cert" field, at which point it gives a similar message about not knowing what the field "cert" is. If i try to omit the "cert" field, however, I get an error for dereferencing a null pointer. I've tried calling the field "certification", "certifications", etc, to no avail. Am I doing something wrong? Seems to me like they messed up, considering the field "proficiency" throws no errors.

OpenStudy (turingtest):

@wio

OpenStudy (turingtest):

I've tried constructing the object in JavaScript then doing JSON.stringify(), but it throws all the same errors.

OpenStudy (turingtest):

Also I originally wrote the POST code with about 5 lines of jQuery, initially, but doing so didn't allow me to inspect the 'mypostrequest' object after running it, so i couldn't tell where the errors were coming from, hence my longer Ajax request.

OpenStudy (e.mccormick):

@dumbsearch2

OpenStudy (anonymous):

I think it is them.

OpenStudy (anonymous):

That or your dates are not formatted correctly.

OpenStudy (turingtest):

thanks, but i just had the interview. the guy i spoke to didn't write or know how the code was written, but he claimed that he had received a few successful applications, though "it wasn't easy, by any means" (which is strange to me because it seems it's supposed to be pretty darn easy to me). maybe he was full of it, I don't know. the dates are formatted as follows: ``` var start1 = new Date("1/1/2011"); start1=start1.toJSON().substr(0, 10); ``` and never threw any errors, so i don't think so

OpenStudy (turingtest):

at least nobody thought i was crazy. I have to log off, thanks for looking at it, guys

OpenStudy (anonymous):

Well, a browser would normally complain about cross domain ajax request.

OpenStudy (turingtest):

i was reading about that on stack overflow, but i didn't really understand how to deal with it. I thought it wasn't applicable to my problem, since the first part of the application process was to use a GET on their url that I then performed the POST on. Since the GET worked fine, I would assume the cross-domain issue would have been dealt with, but I am a total noob to such things, and if you can enlighten me I'd greatly appreciate it. Got to go for now, but if you have any more tips, please share them.

OpenStudy (anonymous):

Use http://www.hurl.it/ or postman to send Ajax requests and see if you can get success.

OpenStudy (turingtest):

well, that worked. So it's the cross-domain problem then... Ok, how to rewrite my code to fix it is the question now, though the interview is over. Still, might be nice if I could get it in quickly.

OpenStudy (turingtest):

at least it gave status code 200, but i need 202. I suppose I got 200 because I used that particular site?

OpenStudy (anonymous):

You can't do cross domain requests with client side Javascript. It just isn't allowed. The best you can do is JSONP, but that require compliance from the server side, because they have to wrap the data correctly.

OpenStudy (turingtest):

well that, they should definitely have, but this whole thing is new to me. I tried adding the header "dataType: "jsonp" but that was insufficient, so I started reading this http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript which led me to this http://www.html5rocks.com/en/tutorials/cors/ which i'm now trying to understand and implement (mainly understand, since this whole lingo is, again, totally new to me)

OpenStudy (anonymous):

Cross-Origin Resource Sharing will only work if the server is set up to allow it. Are you required to do this with client side Javascript?

OpenStudy (turingtest):

No, but I am posting to a special url that was created with a GET in the previous procedure, so I would assume they are set up to allow it. Any suggestions?

OpenStudy (anonymous):

You can make a GET request with any language that allows HTTP requests. I wouldn't assume that such would be a green light for AJAX. Don't use a browser to do this. Use a normal programming language.

OpenStudy (turingtest):

I jsut did the get by writing directly into the url, is that what you mean by using the browser? so I should go back and do the GET with javascript or something? or will we run intothe same cross-domain issues? Strange because doing the get directly into the url gave me theirl little email confirmation of "you've donethis right, onto step 2...."

OpenStudy (turingtest):

my space bar sucks, sorry for the illegibility

OpenStudy (anonymous):

Typing something into the address box is not cross-domain. The browser itself has no domain. The domain for a Javascript file is going to be whatever server hosted it, or localhost if you created on your own computer.

OpenStudy (anonymous):

If you only need to do this one time, then use postman. http://www.getpostman.com/ hurlit, or curl.

OpenStudy (turingtest):

ok thanks, i'll try that. I didn't even know what xml was until i saw this application, and most solutions i saw on stack overflow involved it, so using this postman thing sounds promising. I'll give it a go and let you know how it went. Thanks again

OpenStudy (anonymous):

The thing to understand is that any computer can make an HTTP request to whatever domain. However, Javascript when used in a browser has to be very restrictive of what it lets you do. For example it can't start creating files on your hard drive or anything like that. It can't just open up a socket and start talking to someone else on it. AJAX lets you do HTTP requests, with the restriction that the domain must be the same as the domain which served the page you are on.

OpenStudy (turingtest):

I'm starting to get this pieced together. It certainly makes sense that there would be such restrictions on js. As far as you saying "AJAX lets you do HTTP requests, with the restriction that the domain must be the same as the domain which served the page you are on." I come to realize i don't have an understanding of the difference between the domain, the url, the host, and the server. I vaguely understand that a server could have many domains on it... right? Clearly I need to study the definitions of each of these things.

OpenStudy (turingtest):

the domain that served me the page was the website i got the application from, so what do you mean by "the domain must be the same as the one which served the page"? the domain of the request must be the same as the one that served the page... I'm not sure I understand what that means :( oh i hate feeling so confused.

OpenStudy (turingtest):

ok looked up the definitions on those things i mentioned, so at least I understand that now. Server can have many domains, domain+path = url, host is just a server with a particular domain, is that right?

OpenStudy (anonymous):

Yes, that sounds about right.

OpenStudy (anonymous):

You would have to modify the code being served by `mkpartners.secure.force.com` for it not to be cross-domain.

OpenStudy (turingtest):

I see, am i headed down the right path to do that? Or should I be looking into some other topics as well?

OpenStudy (turingtest):

but... what code have they served me? where would I access it?

OpenStudy (anonymous):

They haven't served any code.

OpenStudy (anonymous):

I'm assuming you created a javascript file and an html file on your own computer and just ran it.

OpenStudy (anonymous):

When you made a get or post request with them, the response is essentially what they served you.

OpenStudy (turingtest):

right, so I have to deal with the cross domain issue then. yes, that's exactly what i did, which, as you said, makes it cross domain. So I have to overcome that. You say jsonp is the only way to go, or just recommended, or...?

OpenStudy (anonymous):

The way to go is not to use client-side Javascript to do this. For example if you had to make a client-side interface... you would then want to send the data directly to your own server. Then your server could, without any restrictions, make the http request on behalf of the interface. This is what hurlit does.

OpenStudy (turingtest):

So if I sent the data to my own server (I assume that is localhost? or is it my computer's IP ? ), then my server could do the http request because, as you said, the browser has no domain? Is there a way to do this without blindly relying on a 3rd party like hurlit that won't require me to study some vast topic?

OpenStudy (anonymous):

Opening up a page on your own computer is not the same as running a page that was served by your server.

OpenStudy (anonymous):

The server would have its own language, like php, ruby, python, java, or javascript. These languages would be running by the computer, rather than being run by the browser. They do not have to worry about the same-origin policy.

OpenStudy (anonymous):

Basically, this is not something you want to do with a browser unless you have to do so. It's like creating an image editor in a browser.

OpenStudy (turingtest):

So for example, I made a website on my computer and configured apache to access those php files when I type in the name of my website into the address bar. So could you help me understand who is requesting and serving what? Is that an example of opening a page on my computer or running a page served by my server? The latter I assume.

OpenStudy (anonymous):

|dw:1404946437290:dw|

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!