localhost到不通过凭据的不同域

时间:2013-07-09 15:29:41

标签: javascript jquery asp.net ajax cross-domain

我有一个用来调用网页的ajax调用,我想得到结果。它的问题在于它似乎不起作用,并且目前返回 jQuery解析错误

当我查看调试控制台并单击时,它会将我带到我想要的页面,它似乎无法通过ajax执行并存储结果/调用成功。

我的代码:

$.ajax({
  type: "GET",
  url:  "mywebsite",
  data: {username: username, password:password},
  dataType: "jsonp",
  contentType: "application/json",
  xhrFields: {withCredentials: true},
  crossDomain: true,
  success: function(result, status, jqxhr){ alert("success"); },
  error: function(xhr, status, message){ alert(status + ": " + message); }
});

你可以看到我想要添加一些凭据,并且我被告知要将数据存入数据,而不是为用户名密码执行ajax选项

我不确定最新情况,似乎有什么不对劲。我通过堆栈溢出看了3种不同的方法,但似乎没有人给我正确答案。

发布数据

 Request URL:http://10.224.65.5/mas3/DataSources/inspecttech.inspecttech/Schema/Classes?callback=jQuery17202808878293726593_1373384418801&username=njtax.hinspecttech&password=4e1cb7494843c513ee913d122e30ef2fe27635e5f82f575c92d006691906bebbbdb1fe483cfc06b087f7c4070c2879bd3585234a3614a1f04fca8de64ad6bfaf&_=1373384423017
 Request Method:GET
 Status Code:200 OK
 Request Headersview source

请求标题

 Accept:*/*
 Accept-Encoding:gzip,deflate,sdch
 Accept-Language:en-US,en;q=0.8
 Authorization:Basic bmRvdG1vYnguaG1vYmlsZWRldmljZTpzdXBlclNlY3JldERldmljZVBhc3N3b3Jk
 Connection:keep-alive
 Host:10.224.65.5
 Referer:http://localhost:3033/BentleyFormIntegrationFrameset.aspx
 User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36

查询字符串参数查看源视图URL编码

 callback:jQuery17202808878293726593_1373384418801
 username:njtax.hinspecttech
 password:4e1cb7494843c513ee913d122e30ef2fe27635e5f82f575c92d006691906bebbbdb1fe483cfc06b087f7c4070c2879bd3585234a3614a1f04fca8de64ad6bfaf
 _:1373384423017
 Response Headersview source

响应标题

 Cache-Control:no-cache
 Content-Language:en-US
 Content-Length:801
 Content-Type:application/json; charset=utf-8
 Date:Tue, 09 Jul 2013 15:40:22 GMT
 Expires:-1
 Mas-License-Error-Id:NoClientLicense
 Mas-License-Error-Message:Client's license is invalid.
 Pragma:no-cache
 Server:Microsoft-IIS/7.5
 X-AspNet-Version:4.0.30319
 X-Powered-By:ASP.NET

其他信息

预览(在谷歌浏览器中)显示返回的对象,但我不确定它为什么会失败。它看起来像是正确地取出了所有东西。

可能的替代选项通过ASP?

HttpWebRequest request = HttpWebRequest.Create(Request.Url.Scheme + "://" + site +"/DataSources/inspecttech.inspecttech/Objects/DeviceDatabase/" +action) as HttpWebRequest;

NetworkCredential cred = new NetworkCredential(bic_name + "x.h" + user.in_username, user.in_password);
request.Credentials = cred;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
string resp = "";
StreamReader reader = new StreamReader(response.GetResponseStream());
while (!reader.EndOfStream)
    resp += reader.ReadLine();

2 个答案:

答案 0 :(得分:1)

我正在努力解决这个错误,所以我发现了一篇关于CORS(跨源资源共享)和ASP.NET的博客文章。作者还使用ASP.NET Web.API和AJAX调用JQuery发布了一个完整的解决方案。

博文:http://blogs.msdn.com/b/carlosfigueira/archive/2012/07/02/cors-support-in-asp-net-web-api-rc-version.aspx

代码:http://code.msdn.microsoft.com/CORS-support-in-ASPNET-Web-01e9980a

答案 1 :(得分:0)

我假设你没有定义username & password

我不确定你希望这些来自哪里。一方面,您可能需要从他们的字段中获取这些内容,在这种情况下,您需要这样的内容:

$.ajax({
  type: "GET",
  url:  "mywebsite",
  data: {username: $('#username').val(), password:$('#password').val()},
  dataType: "jsonp",
  contentType: "application/json",
  xhrFields: {withCredentials: true},
  crossDomain: true,
  success: function(result, status, jqxhr){ alert("success"); },
  error: function(xhr, status, message){ alert(status + ": " + message); }
});

这将从字段中获取值,假设它们是#username & #password

或者,您只想将它​​们作为字符串传递,在这种情况下您可以这样做:

data: {username: 'username', password:'password'},

以上内容只会使用值username发布值username,同样也会使用密码发布。