Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript aJax REST API调用-Curl在aJax失败时工作_Javascript_Jquery_Ajax_Api_Curl - Fatal编程技术网

Javascript aJax REST API调用-Curl在aJax失败时工作

Javascript aJax REST API调用-Curl在aJax失败时工作,javascript,jquery,ajax,api,curl,Javascript,Jquery,Ajax,Api,Curl,我使用了一个curlapi调用,并将其转换为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}" Ch

我使用了一个curlapi调用,并将其转换为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}"

Chrome错误:


&POST&401 unauthorized&text/html&query.min/js:2脚本不允许跨域ajax调用。解决方案将数据类型更改为jsonp并使用GET方法,但在您的情况下,这可能是不可能的。

好的,我应该如何进行跨域调用?我不需要它是ajax,但客户端将是理想的外部api(您不能更改)。最简单的方法是对服务器进行ajax调用,并在服务器上进行cURL调用。这有点脏,但可以用。
$.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")