无法从jquery调用API。

huangapple go评论64阅读模式
英文:

Not able to call API from jquery

问题

以下是您的JavaScript代码的翻译:

var Id = 32456;

$.ajax({
    type: "POST",
    url: RetrieveValue('href') + '/API/Delete',
    headers: {
        'Authorization': 'Bearer ' + RetrieveValue("Token"),
    },
    data: '{"Id":' + Id+ '}',
    async: true,
    cache: false,
    dataType: "json",
    contentType: "application/json",
    success: function (response) { 
    },
    error: function (error) {                   
    }
});

以下是您的API代码的翻译:

[HttpPost("Delete/")]
public async Task<ActionResult> Delete(long Id)
{
}

如果您传递了值,但ID仍然为0,可能需要检查一下传递的值是否正确,并确保在请求正文中以JSON格式传递了正确的ID值。

英文:

My JS code:

var Id = 32456;

$.ajax({
    type: &quot;POST&quot;,
    url: RetrieveValue(&#39;href&#39;) + &#39;/API/Delete&#39;,
    headers: {
        &#39;Authorization&#39;: &#39;Bearer &#39; + RetrieveValue(&quot;Token&quot;),
    },
    data: &#39;{&quot;Id&quot;:&#39; + Id+ &#39;}&#39;,
    async: true,
    cache: false,
    dataType: &quot;json&quot;,
    contentType: &quot;application/json&quot;,
    success: function (response) { 
    },
    error: function (error) {                   
    }
});

My API code:

    [HttpPost(&quot;Delete/&quot;)]
    public async Task&lt;ActionResult&gt; Delete(long Id)
    {
    }

I am getting ID = 0, even though I have passed value.

Thanks for your help in advance.

答案1

得分: -1

将id设置为查询参数:
url: RetriveValue('href') + '/API/Delete?Id=' + Id

英文:

Set id as query parameter:

url: RetriveValue(&#39;href&#39;) + &#39;/API/Delete?Id=&#39; + Id

huangapple
  • 本文由 发表于 2023年6月16日 14:46:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定