【问题标题】:aJax REST API call - Curl Works while aJax failsaJax REST API 调用 - 当 aJax 失败时 Curl 工作
【发布时间】:2013-08-06 00:22:21
【问题描述】:

我正在使用 cURL api 调用,我已将其转换为 ajax,但它不起作用。有什么想法吗?

curl -v -X POST -H "Content-Type: application/json" -H "Accept: application/json" --user user:pass https://i.am.web/123456789/events -d "{\"name\": \"Marcus0.1\",\"start\": 500000,\"end\": 1361640526000}"

.

$.ajax({
    type: 'POST',
    url: 'https://i.am.web/123456789/events',
    dataType: 'application/json',
    accept: 'application/json',
    async: false,
    username: 'user',
    password: 'pass',
    data: {
        "name": "Marcus0.3",
        "start": 500000,
        "end": 1361640526000
    },
    success: function(){alert('DONE!');},
    error:function(error){alert(error)},
});
alert("ENDING API MAIN FUNCTION")

Chrome 错误:

http://i.am.website/123456789 & POST & 401 未授权 & text/html & query.min/js:2 脚本

【问题讨论】:

    标签: javascript jquery ajax api curl


    【解决方案1】:

    不允许跨域 ajax 调用。解决方案将 dataType 更改为 jsonp 并使用 GET 方法,但在您的情况下这可能是不可能的。

    【讨论】:

    • 好的,我应该如何进行跨域调用?我不需要它是 ajax,但客户端会是理想的
    • 使用外部 api(您无法更改)最简单的方法是对您的服务器进行 ajax 调用,然后在服务器上进行 cURL 调用。它有点脏,但会起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多