如何拥有网页.pypython webservice返回对jquery的jsonp调用

2024-04-29 15:35:26 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想网页.py基于pythonwebservice,在jQuery对列表对象进行ajax调用后,通过JSONP将其发回。

问题是在阅读了大量的例子之后,我仍然不明白如何使它起作用。这是我拿到的密码我在自动取款机上工作:

Javascript代码:

xmlrpcproxy = 'http://0.0.0.0:1885/'; //Back To The Future III reference on the port :)

jQuery.ajaxSetup ({
    url: xmlrpcproxy, // <--- returns valid json if accessed in the browser
    type: "GET",
    cache: false,
    contentType: "jsonp", // Pay attention to the dataType/contentType
    dataType: 'jsonp', // Pay attention to the dataType/contentType
    });
jQuery.ajax({
    success: function(data) {
        console.log("You made it!");
    },
    error: function (xhr) {
        console.log("Error: " + xhr.statusText);
    }
}).done(function(data){
    console.log(data);
    var firstoption = '<option value="select" selected>Please Select</option>';
    jQuery("select#ItemIDSelect").html(firstoption);
    var i;
    var erplist = JSON.parse(data);
    jQuery("responsearea").append(erplist);
    for (i = 0; i < erplist.length; ++i) {
                jQuery("select#ItemIDSelect").append('<option value="' + erplist[i] + '">' + erplist[i] + '</option>');
    }
});

Python网页.py代码

^{pr2}$

示例:b的典型内容

["The Jackson Group's Project", "Research & Development", "E-Learning Integration", "Website Design Templates", "Data Import/Export Plugin", "1", "Project XXX", "Contract Agrolait", "Project : Agrolait"]

有人能帮忙吗?据我所知,有一种方法可以在javascript端设置函数名,所以这可能是解决问题的一种方法,将其设置为我的意思是某个函数。但是,所有解决这一问题的方法都受到ofc的欢迎,到目前为止,我尝试的方法都没有奏效>;<

谢谢你的阅读!在


Tags: the方法projectlog网页datavarfunction
1条回答
网友
1楼 · 发布于 2024-04-29 15:35:26

JQuery似乎在callback查询参数中提供了回调函数名。并确保设置正确的内容类型标头。在

callback_name = web.input(callback='callback').callback
web.header('Content-Type', 'application/javascript') 
return '%s(%s)' % (callback_name, b)

相关问题 更多 >