执行Cross Origin Put请求有哪些成功的方法? 我成功地使用代理发出了一个GET请求并将其放入下拉列表中,如此处所示>> 从API查询创建下拉列表 >>但是在制作PUT请求时无法使用相同的过程?
思考?
What are some successful methods for performing Cross Origin Put requests? I successfully used a Proxy to make a GET request and put it into a Dropdown list as can be seen here >> Create Dropdown list from API Query >>but have not been able to use the same process in making a PUT Request?
Thoughts?
最满意答案
通过在javascript中使用代理,我能够成功获得PUT请求才能工作。
$.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; //options.url = "http://cors.corsproxy.io/url=" + options.url; } });一旦代理建立,我使用chrome扩展(现在是一个桌面应用程序) Postman来获取PUT HTML代码。 这是通过首先让PUT请求在Postman中工作然后选择“代码”链接(在“发送”按钮下面)并从下拉列表中选择“JavaScript Jquery AJAX”来完成的。 以下是Postman输出代码的示例。
var settings = { "async": true, "crossDomain": true, "url": "https://[apiurl].com", "method": "PUT", "headers": { "content-type": "text/xml", "cache-control": "no-cache", "postman-token": "[token]" }, "data": "<this_is_the_xml_data_youre_sending>" } $.ajax(settings).done(function (response) { console.log(response); });从Postman复制代码后,将代理代码和Postman javascript放入HTML页面并观察PUT请求。
I was able to successfully get a PUT request to work just peachy through the use of the proxy in javascript.
$.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; //options.url = "http://cors.corsproxy.io/url=" + options.url; } });Once the Proxy was established, I used the chrome extension (now a desktop app) Postman to get the PUT HTML code. This was done by first getting the PUT request to work in Postman and then selecting the "code" link (below the "send" button) and selecting "JavaScript Jquery AJAX" from the drop-down. Here is an example of outputted code from Postman.
var settings = { "async": true, "crossDomain": true, "url": "https://[apiurl].com", "method": "PUT", "headers": { "content-type": "text/xml", "cache-control": "no-cache", "postman-token": "[token]" }, "data": "<this_is_the_xml_data_youre_sending>" } $.ajax(settings).done(function (response) { console.log(response); });Once the code is copied from Postman, put the proxy code and Postman javascript into an HTML page and watch the PUT request happen.
更多推荐
发布评论