weixin_33682790 2016-10-10 15:23 采纳率: 0%
浏览 184

AJAX API PUT请求

Am I even close to success? I'm trying to use this block of code is jsfiddle to use a PUT request to the API, when using www.hurl.it with the same URL & XML, it succeeds, (woohoo!) but when I try it using this code I have no such luck.

Selecting "RUN" is jsfiddle yields nothing, as in nothing happens at all except the page flashing.

var url = 'https://api.example.com/v1.svc/results/modules/[moduleID]?apikey=[apikey]&source=[source]'

var xmldata = '<ModuleResult><CourseId>JJxblllJXcw1</CourseId><UserId>XaWpNO10m-M1</UserId><Score>100</Score> <Completed>false</Completed><UpdatedAt>2030-04-30T15:36:30</UpdatedAt><Note>JIL</Note></ModuleResult>'

$.ajax({
  url: url,
  type: 'PUT',
  contentType: 'application/XML',
  data: xmldata,
  success: function(data) {
    alert('Load was performed.');
  }
});

Any input would be greatly appreciated!

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33734785 2017-07-20 14:24
    关注

    The call wall being completed Cross Origin, the simple proxy prefilter shown below solved my issue.

    $.ajaxPrefilter( function (options) {
      if (options.crossDomain && jQuery.support.cors) {
        var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
        options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
      }
    });
    

    Note:
    A public proxy is not known for being extremely secure, whoever controls the proxy can see all of your data being pushed across it, so ensure the data is not sensitive if using a public proxy.

    评论

报告相同问题?