簡體   English   中英

Javascript Ajax調用不起作用?

[英]Javascript Ajax call not working?

我正在嘗試使用Ajax調用發出POST請求,但我收到狀態碼為0的錯誤,其中所有請求參數都在高級REST客戶端中工作。

mycode的:

<button>Post</button>

$('button').click(function(e) {
e.preventDefault();

$.ajax({
        url: "https://api.sendgrid.com/v3/mail/send",
        type: "POST",
        contentType : 'application/json',
        headers : {
            'Authorization' : 'Bearer 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',
            'Content-Type' : 'application/json'
        },
        data: JSON.stringify({
              "personalizations": [{
                "to": [{
                  "email": "test@example.com"
                }]
              }],
              "from": {
                "email": "abcd@gmail.com"
              },
              "subject": "Great, World!",
              "content": [{
                "type": "text/plain",
                "value": "Cool OK!"
              }]
            }),
        dataType: "json",           
        success: function (response) {
            var resp = JSON.parse(response)
            alert(resp.status);
        },
        error: function (xhr, status) {
            alert("error : "+status+" :: "+JSON.stringify(xhr));
        }
    });
 });

這是我上面的代碼,運行它時繼續拋出錯誤說

{"readyState":0,"responseText":"","status":0,"statusText":"error"}

還要檢查jsfiddle

請幫幫我,建議我一些解決方案,我無法在代碼中找到問題。

瀏覽器擴展不像瀏覽器本身那樣限於跨源請求。 因此,允許像Advance Advance Client或Postman這樣的擴展請求跨源資源。 請參閱Chrome中的這篇文章: https//developer.chrome.com/extensions/xhr

因此,當您創建一個想要從另一個域訪問資源的網站時,您必須啟用CORS,就像Matt所說的那樣。

好像可能是CORS問題?

如果你有興趣了解科爾。 https://www.html5rocks.com/en/tutorials/cors/

https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html這沒有說明是否可以訪問ajax,因此可能無法通過直接的ajax請求執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM