【问题标题】:Jquery Ajax call problemn with Data Type数据类型的 Jquery Ajax 调用问题
【发布时间】:2012-03-27 10:24:06
【问题描述】:

我正在使用 Jquery 尝试从 Open Calais api 获取一些数据,但如果我无法获得有效响应。如果我使用数据类型“脚本”,我会收到“缺少 ; before statement”的错误。如果我使用数据类型“xml”或其他任何内容,我会从 Open Calais 服务器收到 403 错误。

我决定根据Calais forum post 上的最后一个条目尝试“脚本”数据类型

请不要对我的代码进行批评和批评。我边走边靠。

我的代码:

var baseUrl="http://api.opencalais.com/enlighten/calais.asmx/Enlighten";
var licenseID="wt8h3w3pt333eewdwsyuhut6";
var content="In response to a legislative provision in a bill reauthorizing the FAA, the agency has launched a comment period as it selects six test sites to evaluate unmanned aircraft systems. The focus of the proceeding will be determining the location of the test sites along with establishing...";
var PARMS="&contentType=text/xml&outputFormat=xml/rdf"
var PostDatavar = "?licenseID="+licenseID+"&content="+encodeURIComponent(content)+PARMS;
var componentURL=baseUrl+PostDatavar;

function sendIt(sendData){
$.ajax({
    url:componentURL,
    type: "POST",
    dataType:"script",
    success:function(data){
            alert(data)); 
            console.log(data);
    },
    error:function(){
    alert("it's broken");
    }}
);

}

【问题讨论】:

    标签: jquery xml ajax api opencalais


    【解决方案1】:

    这是因为同源策略。您不能对跨域请求使用简单的 ajax 请求。有关更多信息,请参阅我对this question 的回答。您可以关注this tutorial,了解有关如何解决此问题的更多信息。

    【讨论】:

    • 当然!感谢您指出了这一点。我将通过 php 运行它并返回到这里。
    【解决方案2】:


    请使用以下函数,因为上面发布的脚本中编写的函数存在一些语法错误。

    function sendIt(sendData){
    $.ajax({
        url:componentURL,
        type: "POST",
        dataType:"script",
        success:function(data){
                alert(data); 
                console.log(data);
        },
        error:function(){
        alert("it's broken");
        }}
    );
    }
    

    【讨论】:

    • 您发布的代码与我的相同。我错过了什么吗?
    • 是的,使用的大括号的打开和关闭有一些问题。
    猜你喜欢
    • 2017-06-16
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多