Bill Software Engineer
Bill Software Engineer

Reputation: 7782

Posting content to Facebook API using Javascript

I am trying to post to Facebook API using Javascript. It works well when the content is hard coded in Javascript, but when I gather the content from a textbox entered by user. It throw a Same-origin policy error:

This is where I do the posting.

        FB.api('/1494363804210145/feed', 'post', postData, 
        function(response) {
            console.log(JSON.stringify(response));
            $("#status").val("Your Message as been posted!");
        });

If postData is hardset, it works:

        var postData = { 
            message      : "TEST",
            name         : "TEST",
            from         : 'pageid',
            access_token : pageAccessToken,
            description  : "TEST"
        };

If postData is dynamic, it does not work:

        var postData = { 
            message      : $("#postMessage"),
            link         : $("#postLink"),
            picture      : $("#postImage"),
            name         : $("#postTitle"),
            from         : 'pageid',
            access_token : pageAccessToken,
            description  : $("#postDesc")
        };

I am not using IFrame of any kind. How do I fix this?

Upvotes: 2

Views: 63

Answers (1)

codeandcloud
codeandcloud

Reputation: 55200

You need to append .val(). Right now you are trying to post the jQuery object.

var postData = { 
    message      : $("#postMessage").val(),
    link         : $("#postLink").val(),
    picture      : $("#postImage").val(),
    name         : $("#postTitle").val(),
    from         : 'pageid',
    access_token : pageAccessToken,
    description  : $("#postDesc").val()
};

Upvotes: 2

Related Questions