weixin_33695450 2014-12-12 10:01 采纳率: 0%
浏览 31

向ajax发出发布请求

Trying to get the Zendesk API to work

curl https://{subdomain}.zendesk.com/api/v2/tickets.json \
  -d '{"ticket":{"subject":"My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}' \
  -H "Content-Type: application/json" -v -u {email_address}:{password} -X POST

Works fine, when I convert it to ajax:

var http = "https://SSS.zendesk.com/api/v2/tickets.json";
var data = {"ticket":{"subject":"My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}};


$.ajax({
    url: http,
    beforeSend: function(xhr) { 
      xhr.setRequestHeader("Authorization", "Basic " + btoa("user:pass")); 
    },
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    processData: false,
    data: data,
    success: function (data) {
      console.log(JSON.stringify(data));
    },
    error: function(){
      console.log("Cannot get data");
    }
});

I get error:

XMLHttpRequest cannot load https://quran.zendesk.com/api/v2/tickets.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://null.jsbin.com' is therefore not allowed access. The response had HTTP status code 422.

How can I fix this?

  • 写回答

1条回答 默认 最新

  • weixin_33743661 2014-12-12 10:04
    关注

    You are falling foul of the Same Origin Policy.

    You cannot make AJAX requests in jQuery to third party domains unless using JSONP or CORS (where they are supported - check their documentation for information).

    If the URL you are trying to retrieve data from does not support either of those you would need to make a request via your local server, and retrieve the response from that via JS.

    评论

报告相同问题?

悬赏问题

  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!