How to get a cross-origin resource sharing (CORS) post request working

后端 未结 11 2167
野趣味
野趣味 2020-11-21 22:35

I have a machine on my local lan (machineA) that has two web servers. The first is the in-built one in XBMC (on port 8080) and displays our library. The second server is a

11条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 23:34

    I had the exact same issue where jquery ajax only gave me cors issues on post requests where get requests worked fine - I tired everything above with no results. I had the correct headers in my server etc. Changing over to use XMLHTTPRequest instead of jquery fixed my issue immediately. No matter which version of jquery I used it didn't fix it. Fetch also works without issues if you don't need backward browser compatibility.

            var xhr = new XMLHttpRequest()
            xhr.open('POST', 'https://mywebsite.com', true)
            xhr.withCredentials = true
            xhr.onreadystatechange = function() {
              if (xhr.readyState === 2) {// do something}
            }
            xhr.setRequestHeader('Content-Type', 'application/json')
            xhr.send(json)
    

    Hopefully this helps anyone else with the same issues.

提交回复
热议问题