开发者

JSONP call with server-side language as Javascript

开发者 https://www.devze.com 2023-03-21 16:53 出处:网络
I\'ve been trying to use JSONP to get a JSON object from a server via a client-side call (on a different port). However, because my server is implemented in javascript using Node.js and Express, I hav

I've been trying to use JSONP to get a JSON object from a server via a client-side call (on a different port). However, because my server is implemented in javascript using Node.js and Express, I haven't been able to find much on JSONP with Javascript on the server as most sites I found used php for server-side code.

I believe the issue is with how I set up the url with respect to the callback, which I'm a bit fuzzy on cause it's new to me.

On the server:

//Setting up server stuff
var express = require('express'),
  app = express.createServer();
  app.use(express.logger());

//Making a connection to the mongoDB to get the data that I want to display in the JSON object
new Db('prism', 
   new Server("127.0.0.1", 27017, {auto_reconnect: false}), {}).open(function(err, db) {
   app.get('/getData', function(req, res) {
      console.log('JSONPCALLBACK CALLED WITH RETURNDATA PASSED IN; SERVER SIDE');
        if (typeof callback == 'function') {
        console.lo开发者_开发问答g('callback is defined');
        callback(returnData);
      }
      else {
        console.log('callback is not defined');
      }
   }
});

And on the client:

$.ajaxSetup({ cache : false });
$.getJSON('http://127.0.0.1:1337/getData&callback=?', function(rtndata) {
  console.log('SUCCESS');
  console.log(rtndata);
 });

embedded by the standard tags.

But I get the error:

GET http://127.0.0.1:1337/getData&callback=jQuery16108897686484269798_1311007334273?_=1311007334342 404 (Not Found)

The server is on port 1337 while the client is run through MAMP on localhost:8888. I'm not sure if its even a localhost related issue as I've been trying to get this setup running for a few days now.

I believe the issue has something to do with not writing this line, which is in php, into my server-side Javascript. Most of the JSONP examples I found had something like this. But I'm not sure.

if ($GET['callback'] != '') 
   $json = $GET['callback']."( $json )";
   return $json;

Any help would be greatly appreciated. I apologize ahead of times for being super verbose.

Bests, Cong


I think you have two problems. First is the 404. Completely separate from getting the client-side jQuery code to work, you need to make sure that you can issue a regular browser request (i.e. paste in that URL) and get back what you expect. I haven't used express, so it's hard for me to comment on why you'd be getting that, except to say that I don't see 1337 anywhere in your server-side code, just what appears to be the port number 27017.

The second problem is that you don't actually want to execute the callback on the server, just build the JSON response including the callback (string) prefixed to it.

So instead of this ...

  if (typeof callback == 'function') {
    console.log('callback is defined');
    callback(returnData);
  }
  else {
    console.log('callback is not defined');
  }

try this:

  if (callback) {
    console.log('callback is defined');
    res.write(callback + '(' + JSON.stringify(returnData) + ')');
  }
  else {
    console.log('callback is not defined');
  }

Hope this helps!


From http://api.jquery.com/jQuery.getJSON/ there is an example that includes 2 '?' in the URL.

you only have one, so try

$.getJSON('http://127.0.0.1:1337/getData?callback=?', function(rtndata) {

and see if that gets rid of your 404 then look @jimbojw suggestion for returning a proper jsonp formated responce.


Use this:

var express = require("express");
var server = express.createServer();
server.enable("jsonp callback");
server.get("/foo", function(req, res) {
    // this is important - you must use Response.json()
    res.json("hello");
});

jsonp with node.js express

0

精彩评论

暂无评论...
验证码 换一张
取 消