ajax response for node file - ajax

so still a newb to nodeJS and back end code in general. I need some help with ajax and node. For instance I have a function that goes like this
function random(response) {
var objToJson = {...};
response.write(objToJson);
response.end();
}
if instead of writing I want to pass this json object to another function as a response of an ajax call made to it how would that be?
Thanks for you help!

Node.js allows you to easy manipulate HTTP request and response objects.
Ajax still sends a HTTP request object and the Ajax onsuccess callback manipulates a HTTP response object.
Writing an object to a response for an ajax request allows your ajax success handler to manipulate that data.
There are abstraction libraries for RPC like now

It sounds like you want to return a javascript object to work with in your client-side code. While that's not possible directly (you can't send an object directly over HTTP; you're always serializing/deserialing in some fashion), you can certainly return a JSON payload and easily convert that to an in-memory javascript object. If that's what you're doing, you should set the response content type to application/json.
response.setHeader("Content-Type", "application/json");
If you're writing "pure" javascript (no framework wrapping XmlHttpRequest), you'll need to eval() the responseText to convert it to an object. If you're using something like jQuery, it will do that work for you (assuming you set the content type as suggested above).

Related

How to access request body in cy.route response callback?

I have built something that can capture network requests and save them to a file. Currently I am now trying to build the second part which should return these captured requests, but running into some difficulties. Sometimes I have multiple requests going to a single method/url combination, and I need to return a different response depending on the request body. The problem I am facing is illustrated in the example below:
cy.route({
url: 'api.example.com/**',
method: myMethod,
response: routeData => {
// I can set the response here
// But I don't have access to the request body
},
onRequest: xhr => {
// I can access the request body here
// But I am not supposed/able to set the response
},
})
If I understand the API docs correctly, I am supposed to set the response in the response callback. However, in that callback I do not seem to have access to the XHR object from which I could read the request body.
Is there a way to access the request body in the response callback?
Or, alternatively, is there a way to set the response from the onRequest callback?
Update: just saw this post which mentions a body property which can be added to the cy.route options object. I don't see this in the cypress route docs so I don't know if this is even a valid option, and I also wouldn't know if making multiple calls to cy.route with an identical method and url, but a different body would produce the correct results. If this was of any use, I would have hoped to have seen some branching logic based on a body property somewhere in this file, so I am not super hopeful.
Cypress v6 comes with the cy.intercept API. Using that is much more convenient than using cy.server and cy.route.

temporarily change $http.defaults.transformResponse

How can I customize $http in angularjs such that it will accept strings as its response in a $http.post call? Right now, when I do a $http.post call, my response is in string but angularjs by default uses JSON therefore I get an error. Right now I have something along the lines of
function getResponseURL(response) {
//this will convert the response to string
return response;
}
$http.defaults.transformResponse = [];
$http.defaults.transformResponse.unshift(getResponseURL);
However if I use the code above, any $http.post calls after that call uses string. I want it to use the original default JSON format. How can I go about into just temporarily changing the response to string for this one call but the rest stay as JSON type as a response?
Why not only register that transform for ONLY that request?
Angular js $http docs
If you wish
override the request/response transformations only for a single
request then provide transformRequest and/or transformResponse
properties on the configuration object passed into $http.

JSON or Form Data? What's the preferred way to send data back to the server

I'm messing around with Django and AngularJS, attempting to send data back to my server using the $http resource. It looks like I could do this by either posting the data back as a form by setting the content-type as follows:
$http({
url: url,
data: form_encoded_data,
method: 'POST',
headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
});
Or post back JSON in the request body with something like this:
$http.post(url, json_data)
.success(function(data, status, headers, config) {
...
}
In the first method, I can get access to the form data in my Django view via request.POST, and in the second, I can get access to the JSON via request.body. They both seem to work, but what's considered best practice?
I'm not sure what the convention for JSON data is. What I am sure of is that there is a convention for getting form data. In the absence of a compelling reason to use JSON, I would tend to think it's better to stick with the request.POST
I would go with using a form, it just makes sense intuitively and it is what I have used every time.
I prefer to use the $http service that accepts an object literal for configuration:
$http({method:'POST',url:'api/customers/add', data: customer})
.success(function(data) {
...
});
The result is promise object where you can immediately call .success. Its cleaner and easier to read IMO.
Note: customer is typically a data-bound object literal in JSON notation, but it does not have to be.

Is Ajax with JSON or Ajax with dojo are same

I am new in Ajax . Want to use Ajax with Json . I am searching the tutorial for this and i find this.
I want to ask is i am in right direction ?
Is both things are same Ajax with Json and Ajax with dojo?
Not the same
Ajax is a technology that send request and accept data asynchronously(do not need to reload page).
You can use JSON or XML to send the data or just use the string.
When you do the Ajax request, any data type(like array, object, number..) except string will lost their data type and become string, so If you want to reserve their type, you must use data transmit format like JSON and XML.
Dojo just a library which have easier method for doing Ajax. You also can use jQuery, Angular,..Whatever even JavaScript native XMLHttpRequest.

JSONP, do you have to change your JSON file?

Can someone help me understand JSONP a little better?
I have a json file being out putted to a url. But due to same-origin policy I need to use JSONP. My question is do I need to change the actual JSON file or will using an ajax call with jquery, dataType: 'jsonp' do the work for me?
JSONP is nothing but, JSON with padding i.e. JSON wrapped by a function call. This format helps to pass the JSON data to java script.
JSON came into picture, when the JSON i.e. java script object can be used to represent the data, which was previously represented in the form of XML.
For example,
var data={...}; is data in json format. Whereas In JSONP, same data is written as getData(data);
In your scenario of ajax call, dataType:'jsonp', json data has to be passed as an argument to a function. You can access the response in that function.
If you could have provided some code, it will be easy to resolve your query. Information about JSONP is available on wikipedia here.
You will have to wrap your JSON data in a function call.
Like, someFunctionName(YOUR_EXISTING_JSON_DATA);
And,
use someFunctionName as jsonp callback
See, Cross-domain communications with JSONP

Resources