2016-10-04 103 views
0

我收到错误无法创建新对象,错误代码为:XMLHttpRequest失败:“无法连接到Parse API”当我尝试连接到Parse Server API。我在Heroku上部署了ParsePlatform/parse-server-example。尝试连接到在Heroku解析这个代码时,我可以访问我的应用程序没有problems.I一个broswser得到错误:使用Heroku上的Parse服务器“无法连接到Parse API”

var $result=$('#results').html('Testing configuration.....'); 
    Parse.initialize('<MY_APP_ID>', '<MY_JAVASRIPT_KEY>'); 
    Parse.serverURL = '<MY_HEROKU_APP_NAME>.herokuapp.com/' 

    var ParseServerTest = Parse.Object.extend('ParseServerTest'); 
    var _ParseServerTest = new ParseServerTest(); 

    _ParseServerTest.set('key', 'value'); 
    _ParseServerTest.save(null, { 
    success: function(_ParseServerTest) { 
     var txt = 'Yay, your server works! New object created with objectId: ' + _ParseServerTest.id; 
     $result.html('<div class="alert alert-success" role="alert">' + txt + '</div>'); 
    }, 
    error: function(_ParseServerTest, error) { 
     var txt = 'Bummer, Failed to create new object, with error code: ' + error.message; 
     $result.html('<div class="alert alert-danger" role="alert">' + txt + '</div>'); 
    } 
    }); 

index.js

// Example express application adding the parse-server module to expose Parse 
// compatible API routes. 

var express = require('express'); 
var cors = require('cors'); 

var ParseServer = require('parse-server').ParseServer; 
var path = require('path'); 

var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI; 

if (!databaseUri) { 
    console.log('DATABASE_URI not specified, falling back to localhost.'); 
} 

var api = new ParseServer({ 
    databaseURI: databaseUri || 'mongodb://localhost:27017/dev', 
    cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js', 
    appId: process.env.APP_ID || 'myAppId', 
    masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret! 
    serverURL: process.env.SERVER_URL || 'https://localhost:1337/parse', // Don't forget to change to https if needed 
    liveQuery: { 
    classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions 
    } 
}); 
// Client-keys like the javascript key or the .NET key are not necessary with parse-server 
// If you wish you require them, you can set them as options in the initialization above: 
// javascriptKey, restAPIKey, dotNetKey, clientKey 

var app = express(); 
app.use(cors()); 

// Serve static assets from the /public folder 
app.use('/public', express.static(path.join(__dirname, '/public'))); 

// Serve the Parse API on the /parse URL prefix 
var mountPath = process.env.PARSE_MOUNT || '/parse'; 
app.use(mountPath, api); 

// Parse Server plays nicely with the rest of your web routes 
app.get('/', function(req, res) { 
    res.status(200).send('I dream of being a website. Please star the parse-server repo on GitHub!'); 
}); 

// There will be a test page available on the /test path of your server url 
// Remove this before launching your app 
app.get('/test', function(req, res) { 
    res.sendFile(path.join(__dirname, '/public/test.html')); 
}); 

var port = process.env.PORT || 1337; 
var httpServer = require('http').createServer(app); 
httpServer.listen(port, function() { 
    console.log('parse-server-example running on port ' + port + '.'); 
}); 

// This will enable the Live Query real-time server 
ParseServer.createLiveQueryServer(httpServer); 

Heroku的配置:

Heroku config

我跟着这个帖子:How can I host my own Parse Server on Heroku using MongoDB?除了我没有使用“部署到Eroku”按钮,我手动部署它。

谢谢你的帮助。

回答

0

最后我找到了一种方法。 我首先在我的mongo db中创建了另一个用户,然后在Heroku中进行更改。尝试用相同的js代码code jsfiddle连接,但没有奏效...

然后我试图与Android客户端,这个环节对我帮助很大http://www.robpercival.co.uk/parse-server-on-heroku/

StarterApplication.java

public class StarterApplication extends Application { 
@Override 
public void onCreate() { 
    super.onCreate(); 

    // Enable Local Datastore. 
    Parse.enableLocalDatastore(this); 

    // Add your initialization code here 
    Parse.initialize(new Parse.Configuration.Builder(getApplicationContext()) 
      .applicationId("BUTYcVjD7nFz4Le") 
      .clientKey("XgQaeDY8Bfvw2r8vKCW") 
      .server("https://xxxxx-xxxx-xxxxx.herokuapp.com/parse") 
      .build() 
    ); 

    ParseUser.enableAutomaticUser(); 
    ParseACL defaultACL = new ParseACL(); 
    // Optionally enable public read access. 
    // defaultACL.setPublicReadAccess(true); 
    ParseACL.setDefaultACL(defaultACL, true); 
} 

MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

    ParseAnalytics.trackAppOpenedInBackground(getIntent()); 

    ParseObject test = new ParseObject("Test"); 
    test.put("username","pedro"); 
    test.put("age",33); 
    test.saveInBackground(new SaveCallback() { 
     public void done(ParseException e) { 
      if (e == null) { 
       Log.i("Parse", "Save Succeeded"); 
      } else { 
       Log.e("Parse", "Save Failed"); 
      } 
     } 
    }); 

} 

我真的不知道我的第一个用户有什么问题,无法连接它。我从来没有可以连接的JS代码...但无论如何,我的目标是连接到Android客户端,所以...