开发者

stumped on jquery call inside chrome extension

开发者 https://www.devze.com 2022-12-27 07:00 出处:网络
In my chrome extension I call this: jsonPost = { email:\"phil@demo.demo\", password:\"demo\", content: $(\'#selected_text\').val(),

In my chrome extension I call this:

jsonPost = {
    email:"phil@demo.demo",
    password:"demo",
    content: $('#selected_text').val(), 
    n开发者_如何学Pythonotification_type: $('#notification_type').val(),
    name:$('#notification_name').val()
}

$.post('http://localhost:3000/api/create.json', jsonPost, 
        function (data) {
            console.log("type of data = " + typeof(data));
            console.log("data in function = " + data);
            }

The data makes it to the server. But the response is lost, in the console ---type of data = String ---data in function =

So for some reason I am not getting the response back. Works from the browser. I even tried doing a get against cnn.com and got no response.

any ideas?

thanks


Are you running into cross-site-scripting restrictions? If it works in a page that's on "localhost", but not in the extension, it's probably that you need to ask Chrome's permission to access things outside the extension's default security context. See if Google's advice on requesting cross-origin permissions for an extension helps.


you need to use json as the post type, for shorthand method, use it like this

$.post(url, data, function(result) {

}, 'json');


jQuery will take a "best guess" at how to interpret the data based on the MIME type and some other factors, could it be guessing wrong? You can force it by adding a fourth parameter to your call specifying the data type — e.g., "json", "text", "html"... Full list here on the ajax docs.

0

精彩评论

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

关注公众号