我在jomsocial环境中创建了Restful web服务。它在本地机器上。当我使用REST控制台测试它时,它会返回响应。
形成的本地url是…
http://localhost:5454/kisan-06/index.php?option=com_api&format=raw&app=users&resource=login&key=dfd8a84f8cdce807ae1d30a838415ea37eaa075c
问题:
当我使用jQuery.ajax调用相同的函数时,总是调用错误回调函数。
Ajax调用是…
$.ajax({
type: "POST",
url: "http://localhost:5454/kisan-06/index.php?option=com_api&format=raw&app=users&resource=login&key=dfd8a84f8cdce807ae1d30a838415ea37eaa075c",
data: "{ username: 'sai.kiks2@gmail.com', password: '123456'}",
contentType: "application/json; charset=utf-8",
cache : false,
dataType: "json",
success: function(data) {
alert("in success");
},
error: function(jqXHR,error, errorThrown){
//alert("There was an error loggin in");
alert("responseXML ",jqXHR.responseXML);
alert("responseText: ",jqXHR.responseText);
alert("errorThrown: ",errorThrown);
}
});
我有一个asp.net web服务,它返回的响应是…
<?xml version="1.0" encoding="utf-8"?>
<string>{"UserID":"180339206","LogInName":"Amol Chakane","IsValid":"True","UserRoleID":"1","IsPending":"0","IsOrganization":"False"}</string>
来自jomsocial web服务的响应是…
["UserID : 475", "LogInName : kruti patil", "IsValid : True", "UserRoleID : 1", "IsPending : 0", "IsOrganization : False"]
我搜索了这个问题,但找不到解决方案。请帮我一下。
编辑#1
尝试获取其他格式的响应。
{
"UserID": "475",
"LogInName": "kruti patil",
"IsValid": "Yes",
"UserRoleID": "1",
"IsPending": "NO",
"IsOrganization": "No",
}
但它仍然不起作用:(
感谢
当您发布更多信息时,我认为我发现了您遇到麻烦的原因。根据这个页面,json对象的语法是错误的。链接页面显示了Javascript中最常见的数据类型的示例。如果你阅读了这些例子,请注意引号的位置。
经过大量搜索,我终于找到了这个问题的解决方案。:)
本地主机需要使用1.0.2.2此网络地址,而不是localhost因为android模拟器无法识别localhost请参阅此
现在至少成功回调被称为
ajax调用是..
$.ajax({
type: "POST",
url: "http://10.0.2.2:5454/kisan-06/index.php?option=com_api&format=raw&app=users&resource=login&key=dfd8a84f8cdce807ae1d30a838415ea37eaa075c",
data: {username: 'sai.kiks2@gmail.com', password: '123456'},
success: function(data, textStatus, jqXHR) {
alert("in success");
},
error: function(jqXHR, textStatus, errorThrown){
alert("There was an error loggin in");
}
});
但现在我面临另一个问题。。。
如何访问成功回调中返回的数据?