Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7821703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:43:37+00:00 2026-06-02T07:43:37+00:00

I’ve been playing with node.js and then came across the express framework. I can’t

  • 0

I’ve been playing with node.js and then came across the express framework. I can’t seem to get it to work when different ports are being used.

I have my ajax on http://localhost:8888 which is a MAMP server I’m running on my Mac.

$.ajax({
    url: "http://localhost:1337/",
    type: "GET",
    dataType: "json",
    data: { }, 
    contentType: "application/json",
    cache: false,
    timeout: 5000,
    success: function(data) {
        alert(data);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert('error ' + textStatus + " " + errorThrown);
    }
});

As you can see my node.js server is running on http://localhost:1337/. As a result nothing is getting returned and it’s throwing an error.

Is there a way around this?

Thanks

Ben

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:43:38+00:00Added an answer on June 2, 2026 at 7:43 am

    The problem you have is that you are trying to make a cross-origin request and that’s not allowed by browsers (yes, the same hostname with a different port counts as a different origin for this purpose). You have three options here:

    1. Proxy the request.

    Do this one if you can. Write some code that runs on the :8888 server
    that proxies requests to the 1337 one. You can also do this by
    sticking a proxy in front of both of them, something like Nginx is
    pretty good at this and easy to set up

    2. Use CORS (Cross Origin Resource Sharing)

    See: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing and https://developer.mozilla.org/en/http_access_control

    This means adding some headers to your responses to tell the browser that cross-origin requests are ok here. In your Express server add middleware like this:

      function enableCORSMiddleware (req,res,next) {
         // You could use * instead of the url below to allow any origin, 
         // but be careful, you're opening yourself up to all sorts of things!
         res.setHeader('Access-Control-Allow-Origin',  "http://localhost:8888");
         next()
      }
     server.use(enableCORSMiddleware);
    

    3. Use JSONP

    This is a trick where you encode your “AJAX” response as Javascript code. Then you ask the browser to load that code, the browser will happily load scripts cross-origin so this gets round the cross-origin issue. It also lets anyone else get round it as well though, so be sure that’s what you want!

    On the server side you need wrap your response in a Javascript function call, express can do this for automatically if you enable the “jsonp callback” option like this:

    server.enable("jsonp callback");
    

    Then send your response using the “json()” method of response:

    server.get("/ajax", function(req, res) {
        res.json({foo: "bar"});
    });
    

    On the client side you can enanble JSONP in jQuery just by changing “json” to “jsonp” in the dataType option:

    dataType: "jsonp", 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.