我有一个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();
我也遇到了同样的错误,所以我找到了一篇关于使用ASP.NET进行CORS(Cross-Origin Resource Sharing)的博客文章。作者还发布了一个使用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
我假设您尚未定义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
,并且同样具有密码。