Sending ajax get request to node, response is undefined


Randy

I have a node server running a single web page. The page has a contact form with a send button.

If I click send, the following code runs:

$('.contactsend').click(function(){
    $.ajax({
        method: "GET",
        url: "http://127.0.0.1:3000/contact",
        dataType: "jsonp"
    })  .done(function(data) {
            alert( data );
        })
        .fail(function() {
            alert( "error" );
        })
        .always(function() {
            alert( "complete" );
        });
})

Seems ok so far, reached the page.

This is what the middleware does:

router.get('/', function(req, res, next) {
  res.send({test:'test'})
  //res.send('test')
});

After sending this message, the web page displays the following message:

Uncaught SyntaxError: Unexpected token :

If I don't send , jsonbut a string, the message is like

Uncaught ReferenceError: test is not defined
Angler

The client is expecting jsonp, but you are sending json.

Try this:

router.get('/', function(req, res, next) {
    res.jsonp({test:'test'});
});

Related


Frontend undefined ajax get request response

Carlin 89 I am a novice coder. I am trying to insert var cPrice in my front end HTML page. However, it is undefined and I don't know why. ///////////// Display Pop-up finciton ////////////////// //Start// $('#button1').on('click', function() { // Show the pop

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

Sending JSON object on AJAX request, undefined on server

David I am using XMLHttpRequest like this: xml.send(JSON.stringify({ingredients: this.state.ingredients})); Send the object ( this.state.ingredients) to the server. I'm pretty confident it's being sent correctly because under the "Network" tab in Chrome Dev T

Sending JSON object on AJAX request, undefined on server

David I am using XMLHttpRequest like this: xml.send(JSON.stringify({ingredients: this.state.ingredients})); Send the object ( this.state.ingredients) to the server. I'm pretty confident it's being sent correctly because under the "Network" tab in Chrome Dev T

Sending JSON object on AJAX request, undefined on server

David I am using XMLHttpRequest like this: xml.send(JSON.stringify({ingredients: this.state.ingredients})); Send the object ( this.state.ingredients) to the server. I'm pretty confident it's being sent correctly because under the "Network" tab in Chrome Dev T

Sending JSON object on AJAX request, undefined on server

David I am using XMLHttpRequest like this: xml.send(JSON.stringify({ingredients: this.state.ingredients})); Send the object ( this.state.ingredients) to the server. I'm pretty confident it's being sent correctly because under the "Network" tab in Chrome Dev T

Sending JSON object on AJAX request, undefined on server

David I am using XMLHttpRequest like this: xml.send(JSON.stringify({ingredients: this.state.ingredients})); Send the object ( this.state.ingredients) to the server. I'm pretty confident it's being sent correctly because under the "Network" tab in Chrome Dev T

Sending JSON object on AJAX request, undefined on server

David I am using XMLHttpRequest like this: xml.send(JSON.stringify({ingredients: this.state.ingredients})); Send the object ( this.state.ingredients) to the server. I'm pretty confident it's being sent correctly because under the "Network" tab in Chrome Dev T

Sending JSON object on AJAX request, undefined on server

David I am using XMLHttpRequest like this: xml.send(JSON.stringify({ingredients: this.state.ingredients})); Send the object ( this.state.ingredients) to the server. I'm pretty confident it's being sent correctly because under the "Network" tab in Chrome Dev T

Ajax get undefined response data

min2bro I'm trying to parse the Json string returned by an ajax method, but it shows undefined when warning lat,long values, but when I print the console log, the data is displayed correctly there. Can anyone help here on how to parse the GET response data $.a

Ajax get undefined response data

min2bro I'm trying to parse the Json string returned by an ajax method, but it shows undefined when warning lat,long values, but when I print the console log, the data is displayed correctly there. Can anyone help here on how to parse the GET response data $.a

Ajax get undefined response data

min2bro I'm trying to parse the Json string returned by an ajax method, but it shows undefined when warning lat,long values, but when I print the console log, the data is displayed correctly there. Can anyone help here on how to parse the GET response data $.a

Ajax get undefined response data

min2bro I'm trying to parse the Json string returned by an ajax method, but it shows undefined when warning lat,long values, but when I print the console log, the data is displayed correctly there. Can anyone help here on how to parse the GET response data $.a

Get response text of ajax request

Encapsulation Hi, I'm using the message API and I'm having trouble retrieving the answer that appears in my mozilla netblock. below my code function sendMessage(params){ var url = 'https://api.1s2u.io/bulksms?username='+ params.username +'&password

get response after ajax request

best friend I am new to ajax, but I think my problem is simple. After submitting the form, I want the server to echo back to me. Here is my current code that doesn't work. I'm fairly sure the problem is with my ajax as I know my values are being placed in the

get response after ajax request

best friend I am new to ajax, but I think my problem is simple. After submitting the form, I want the server to echo back to me. Here is my current code that doesn't work. I'm fairly sure the problem is with my ajax as I know my values are being placed in the

Node.js - Response to POST request is undefined

Guest 1997 I am learning nodejs and have made a simple site to learn to handle POST requests. Here is my code: Browser side: function sendRequest (params) { var xhr = new XMLHttpRequest(); var url = 'result'; xhr.open("POST",url,true); xhr.setRequestH

why jquery ajax is not sending data in get request

cdm014 I'm setting up a page where our staff can view a list of items that should be on the shelf and then confirm if those items are there, or raise an alert if something goes wrong. I'm trying to create a function for each case (item exists, item doesn't exi

Angular 7 HttpClient get request undefined response

Uros Kalajdzic GET request from API is undefined error The data is displayed, but the console shows an undefined error at line 46: error TypeError: Cannot read property 'detaljne' of undefined and error context PostComponent.html:46 JSON nested in JSON server

Angular 7 HttpClient get request undefined response

Uros Kalajdzic GET request from API is undefined error The data is displayed but the console shows an undefined error at line 46: error TypeError: Cannot read property 'detaljne' of undefined and error context PostComponent.html:46 JSON nested in JSON server T

jQuery ajax not sending response

repair I am sending data to a specific url, then I try to get it using ajax, it just prints out an error <script type="text/javascript" src="{{ url_for('static', filename='jquery.js') }}"></script> <script> $(document).ready(function() { $.ajax({

Error with AJAX GET request despite getting response

call substitution I've set up a GET request to retrieve some JSON from my website's API. Although the response and 200 status are displayed correctly in the network tab of the console (image attached), it still errors out. I've run the JSON response through th

Unable to get JSON response form Ajax request

Rahul Patil I have a WordPress feed in Json format ( https://blog.piscessportfishing.com/feed/json/ ) , but reading it using ajax requests is unpleasant. With success I get nothing for the data variable. Instead it gives the error in the alert. Here is my code

Get request data in jQuery's AJAX response

theft roebuck Suppose I make a Dictionary app where I check if a certain word exists. I have a text input and a button: <input type="text" id="word"> <input type="button" id="button"> I want the server to respond with 1(valid word) or 0(invalid word) . In my