如何使用javascript将图像从网站保存到parse.com后端? [英] How to save an image from website to parse.com backend using javascript?

查看:91
本文介绍了如何使用javascript将图像从网站保存到parse.com后端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常了解javascript。我有一个使用parse.com云的iOS应用程序。现在版主需要一个网站平台将数据上传到移动应用程序旁边的parse.com云。我已经设置了解析,当我想在表格中添加一行时,一切正常。但是,当我想以这种方式将图像添加到云中时:

I know very basic of javascript. I have an iOS App using parse.com cloud. Now moderator need a website platform to upload data to the parse.com cloud beside the mobile App. I have set up parse and everything works fine when I want to add a row to the table. However, when I want to add an image to the cloud in this way:

$("#post-form").submit(function(event){

    console.log("submit");
    event.preventDefault();

    var Event = Parse.Object.extend("Event");
    var newEvent = new Event();

    //Get form info
    var currUser = Parse.User.current();
    var eventName = $("#name").val();

    newEvent.set("name",eventName);
    newEvent.set("user",currUser);

    //Get file from form
    var fileElement = $("#post-file")[0];
    var filePath = $("#post-file").val();
    var fileName = filePath.split("\\").pop();
    alert("HERE 1111");
    if(fileElement.files.length > 0){
        alert("HERE 2222");
        var file = fileElement.files[0];
        var newFile = new Parse.File(fileName, file);
        newFile.save({
            success: function() { console.log("success11111");}, 
            error : function(error){ console.log("file save error== "+error.message);}

        }).then(function(theFile){

            newEvent.set("file",theFile);
            newEvent.save(
            {
                success: function(){ console.log("succes222s");}, 
                error : function(error){ console.log("Post file save error= "+error.message);}
            });
        });

    }
    else
    {
        newEvent.save(
        {
            success: function(){ console.log("success33333");}, 
            error : function(error){ console.log("error= "+error.message);}
        });
    }
});

如果没有选择图像,它可以正常工作并向表中添加一行但是当我选择时一个图像,我收到以下错误:

If there is no image selected, it works fine and add a row to the table but when I select an image, I get the following error:

submit
parse-1.6.11.min.js:15 OPTIONS https://api.parse.com/1/1/files/Screen%20Shot%202015-12-16%20at%209.54.30%20am.png 
c @ parse-1.6.11.min.js:15y.ajax @ parse-
1.6.11.min.js:15u.default.setFileController.saveFile @ parse-1.6.11.min.js:13a.value @ parse-1.6.11.min.js:13(anonymous function) @ 
create.html:183f.event.dispatch @ jquery.min.js:3h.handle.i @ jquery.min.js:3
        create.html:1 XMLHttpRequest cannot load 
    https://api.parse.com/1/1/files/Screen%20Shot%202015-12-16%20at%209.54.30%20am.png. Response to preflight request doesn't pass access 
    control check: No 'Access-Control-Allow-Origin' header is present on the requested 
    resource. Origin 'http://mywebsitename.com' is therefore not allowed access. The 
    response had HTTP status code 404.

感谢是否有人能给我一个简单的解决方案因为我只知道将文件上传到云端的基本内容。

Appreciate if anyone can give me an easy solution as I know only the basic to upload file to the cloud.

我遵循了本教程:
Parse File Upload

此解决方案:

SO

但不是他们为我工作。

推荐答案

也许您正在创建跨源HTTP请求,但目标服务器不要打开Access-Control-Allow-Origin标志。

Maybe you are creating a cross-origin HTTP request but the target server do not turn on 'Access-Control-Allow-Origin' flag.

有关详细信息,请查看此处: Access-Control-Allow-Origin标头如何工作?

For more detail, please check here: How does Access-Control-Allow-Origin header work?

这篇关于如何使用javascript将图像从网站保存到parse.com后端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆