ajax GET无法工作(服务数据无法获取)

ajax GET Not working (Service Data not getting)

本文关键字:数据 获取 服务 工作 GET ajax      更新时间:2023-09-26

当我们在浏览器中点击URL URL时,我们得到了数据。我们得到了类似的数据

[{"UserId":"c2fbd9fb-a423-4d33-9ea4-3aa58f7b52cf","UserType":"Parent","OutPutMessage":"Sucess"}].

但我们需要通过ajax调用获取数据。我们尝试过这样的

window.onload = function (){
 $.ajax({
url:'http://janasenanews.com/MyService.asmx/GetUserLoginVerification',
         data: {
             username:"9440325333",
             password :"9440325333"
         },
         type: "GET",
         dataType: 'JSON',
         success:function(data) {
            alert( data);
              //console.log("Here is the data", data);
         }
    });
    }

但是我们得到了两个错误

Failed to load resource: the server responded with a status of 500 (Internal Server
XMLHttpRequest cannot load  Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 500.

请指导我们代码中的错误。

使用更改脚本

window.onload = function (){
$.ajax({
     url:'http://janasenanews.com/MyService.asmx/GetUserLoginVerification',
     data: {
         username:"9440325333",
         password :"9440325333"
     },
     type: "GET",
     dataType: 'JSON',
     success:function(data) {
        alert( data);
          //console.log("Here is the data", data);
     }
});
}

从你问题上的标签来看,你似乎是在从Cordova应用程序发出ajax请求?如果是这样,您需要将Cordova白名单插件添加到您的应用程序中:

cordova plugin add cordova-plugin-witelist

然后在您的config.xml中,您需要为您的web服务添加一个<access>标签:

<access origin="http://janasenanews.com" />

或允许所有请求:

<access origin="*" />

在Android上,您还需要在index.html:中添加内容安全策略(CSP)

<meta http-equiv="Content-Security-Policy" content="default-src 'self' janasenanews.com">

关于这一切的更多信息,请点击此处:https://github.com/apache/cordova-plugin-whitelist