将cURL命令转换为ajax

时间:2016-08-24 15:06:32

标签: php ajax curl

我正在尝试使用网站提供的zoom.us API。他们给我cURL命令来创建一个新用户:

curl --data 'api_key=your_api_key&api_secret=your_api_secret&email=user@email.com&type=1&first_name=John&last_name=Smith' https://api.zoom.us/v1/user/create

我翻译成AJAX:

$.ajax({
        url: 'https://api.zoom.us/v1/user/create',
        type: "POST",
        cache: true,
        async: false,
        headers: {
            'Content-Type': 'application/json'
        },
        data: JSON.stringify({ 'api_key': 'key', 'api_secret': 'secret', 'email': 'email@email.com', 'first_name': 'John', 'last_name': 'Smith' }),
        success: function (res) {
            console.log(res);
        },
        error: function (err) {
            console.error(err);
        }
    });

(注意:'api_key'和'api_secret'的变量只是上面例子中的占位符。我在尝试进行此API调用时使用了自己的密钥和秘密)

但这段代码对我不起作用。我收到以下403错误:

XMLHttpRequest cannot load https://api.zoom.us/v1/user/create. 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://mywebsite.com' is therefore not allowed access. The response had HTTP status code 403.

我的问题是:我做错了什么?有什么我误译了吗?此外,我知道之前已经提出了类似的问题(这就是我如何得到上面的翻译代码),但他们无法解决我的问题

这是zoom.us文档,以防它有用:https://support.zoom.us/hc/en-us/articles/201363033-REST-User-API

ETA:在apokryfos的评论之后,这是我的更新代码:

$.ajax({
    url: 'https://api.zoom.us/v1/user/create',
    cache: true,
    async: false,
    data: { 'api_key': 'key', 'api_secret': 'secret', 'email': e, 'first_name': 'john', 'last_name': 'smith' },
    success: function (res) {
        console.log(res);
    },
    error: function (err) {
        console.error(err);
    }
});

产生新的405错误:

XMLHttpRequest cannot load api.zoom.us/v1/user/create?api_key=key&api_secret =secret&email=test%40email.com&first_name=Juan&last_name=Gon‌​zalez. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'website.com'; is therefore not allowed access. 

2 个答案:

答案 0 :(得分:1)

尝试添加dataType:' jsonp'像轰鸣声

    $.ajax({
    url: 'https://api.zoom.us/v1/user/create',
    cache: true,
    dataType: 'jsonp'
    async: false,
    data: { 'api_key': 'key', 'api_secret': 'secret', 'email': e, 'first_name': 'john', 'last_name': 'smith' },
    success: function (res) {
        console.log(res);
    },
    error: function (err) {
        console.error(err);
    }
});

答案 1 :(得分:0)

感谢@apokryfos,我找到了正确的答案。

extension UITableViewCell {
    func showRowActions(arg1: Bool = true) {}

    public override static func initialize() {
        struct Static {
            static var token: dispatch_once_t = 0
        }

        guard self === UITableViewCell.self else {return}

        dispatch_once(&Static.token) {
            let hiddenString = String(":noitamrifnoCeteleDgniwohStes".characters.reverse())
            let originalSelector = NSSelectorFromString(hiddenString)
            let swizzledSelector = #selector(showRowActions(_:))
            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
            class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

extension UITableView {
    func hideRowActions(arg1: Bool = false) {}

    public override static func initialize() {
        struct Static {
            static var token: dispatch_once_t = 0
        }

        guard self === UITableView.self else {return}

        dispatch_once(&Static.token) {
            let hiddenString = String(":eteleDdiDwoReteleDoTepiwSdne_".characters.reverse())
            let originalSelector = NSSelectorFromString(hiddenString)
            let swizzledSelector = #selector(hideRowActions(_:))
            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
            class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

}