Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript GET请求调用返回错误的结果_Javascript_C#_Jquery_Ajax_Asp.net Core Webapi - Fatal编程技术网

Javascript GET请求调用返回错误的结果

Javascript GET请求调用返回错误的结果,javascript,c#,jquery,ajax,asp.net-core-webapi,Javascript,C#,Jquery,Ajax,Asp.net Core Webapi,我想从jQuery调用.NET Core中的WEB API,如下所示: [HttpGet("GetText")] public async Task<IActionResult> GetText() { try { string welCome = "Test"; JsonSettings = new JsonSerializerSettings { Formatting = Formattin

我想从jQuery调用.NET Core中的WEB API,如下所示:

[HttpGet("GetText")]
public async Task<IActionResult> GetText()
{
    try
    {
        string welCome = "Test";

        JsonSettings = new JsonSerializerSettings
        {
            Formatting = Formatting.Indented
        };

        return Json(welCome, JsonSettings);
    }
    catch(Exception ex)
    {
        throw ex;
    }
}
[HttpGet(“GetText”)]
公共异步任务GetText()
{
尝试
{
字符串welCome=“Test”;
JsonSettings=新的JsonSerializerSettings
{
格式化=格式化。缩进
};
返回Json(欢迎,JsonSettings);
}
捕获(例外情况除外)
{
掷骰子;
}
}
和jQuery调用者:

<script type="text/javascript">
    $(document).ready(function () {
    $.ajax({
        type: 'GET',
        url: "http://localhost:5000/api/mycontroller/GetText?callback=?",
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (data) {
            if (data.success) {
                alert('Success -> ' + JSON.stringify(data.statusText)); 
            }
        },
        error: function (data) {
            alert('Error -> ' + JSON.stringify(data.statusText)); 
        }
        });
    }); 
</script>

$(文档).ready(函数(){
$.ajax({
键入:“GET”,
url:“http://localhost:5000/api/mycontroller/GetText?callback=?",
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
if(data.success){
警报('Success->'+JSON.stringify(data.statusText));
}
},
错误:函数(数据){
警报('Error->'+JSON.stringify(data.statusText));
}
});
}); 
API调用成功,但它似乎会重定向到my Ajax中的错误函数,并以“success”显示错误警报,因为它是
statusText
。我的意思是:错误->“成功”我不知道为什么会发生这种情况

我想打印
欢迎
作为成功结果并在警报命令中。

另外请注意,我是从另一个项目调用这个API的,我的意思是jQuery的AJAX代码在另一个项目中。我不确定这是否重要

jQuery的AJAX调用方路径:
file:///C:/Users/Me/Desktop/Path/index.html

API的地址:
C:\Users\Me\Documents\Visual Studio 2017\Projects\ThisProject\MyAPI

此API的URL:URL:,

请按以下方式尝试C代码:

[HttpGet("GetText")]
public async Task<IActionResult> GetText()
{
    try
    {
        string welCome = "Test";    
        return Ok(new { message = welCome });
    }
    catch(Exception ex)
    {
        throw ex;
    }
}
$.ajax({
       contentType: 'application/json; charset=utf-8',
       dataType: 'json',
       type: 'GET',
       url: 'http://localhost:5000/api/mycontroller/GetText',
       success: function (response) {
       alert('Success' + response.message);
      },
      failure: function (response) {

      }
});
按以下方式尝试C#代码:

[HttpGet("GetText")]
public async Task<IActionResult> GetText()
{
    try
    {
        string welCome = "Test";    
        return Ok(new { message = welCome });
    }
    catch(Exception ex)
    {
        throw ex;
    }
}
$.ajax({
       contentType: 'application/json; charset=utf-8',
       dataType: 'json',
       type: 'GET',
       url: 'http://localhost:5000/api/mycontroller/GetText',
       success: function (response) {
       alert('Success' + response.message);
      },
      failure: function (response) {

      }
});

谢谢你的回答。但是仍然没有任何变化,我仍然没有收到
警报('Success'+response.message)?回调=?
是必需的,应该附加在我的url之后,因为如果没有回调,brwoser中的请求方法将显示为
选项
,而不是
获取
。仅此部分在API调用
错误:函数后执行(数据){alert('Error->'+JSON.stringify(data.statusText));}
同时返回状态为200。可能是您遇到了CORS错误。请确保您已经配置了CORS。什么是CORS?我如何配置它?感谢您的回答。但是没有任何更改,我仍然没有收到
警报('Success'+response.message);
在success函数中,但是正如我所说,我的API正在成功运行。另外,
?回调=?
是必需的,应该附加在我的url之后,因为没有它,brwoser中的请求方法显示为
选项
,而不是
获取
。仅此部分在API调用
错误:函数后执行(数据){alert('Error->'+JSON.stringify(data.statusText));}
返回状态也是200。可能是您遇到了CORS错误。请确保您已经配置了CORS。什么是CORS?我如何配置它?它希望接收json对象,您正在发送字符串back@Sujit.Warrier所以,不应该
返回Json
返回Json吗?删除
?回调=?
会起作用t首先发送一个
选项
请求,然后在dev tools中发送一个
get
请求back@Sujit.Warrier因此,不应该
返回Json
返回Json吗?删除
?回调=?
将起作用。您将看到它首先发送
选项
请求,然后发送se在开发工具中发送一个
get
请求。