[英]Json call (jquery) to a WCF restful service always run error function
I am trying to call my web service (WCF Restful) from a different domain (in the following code they are both in localhost but with different port number which I suppose considered as cross-domain request) using the JavaScript below. 我试图使用下面的JavaScript从另一个域(在以下代码中,它们都在localhost中,但具有不同的端口号,我认为这是跨域请求)调用我的Web服务(WCF Restful)。
jQuery.support.cors = true;
$(document).ready(function () {
{
$.ajax({
type: "POST",
url: "http://localhost:52032/Service1.svc/std",
contentType: "application/jsonp; charset=utf-8",
dataType: "jsonp",
processData: false , //Same result if i remove this line
crossDomain : true , //Same result if i remove this line
converters: window.String, //Same result if i remove this line
success: OnSuccess,
error: OnError});
}
});
function OnSuccess(data)
{
alert("Finaly :D");
JSON.stringify(data);
}
function OnError(request, status, error)
{
alert("Error: "+request.statusText + JSON.stringify(request)+ " | " +JSON.stringify(status) +" | " +JSON.stringify(error));
}
My web service is pretty simple. 我的网络服务非常简单。
getStudents()
returns a JSON string. getStudents()
返回JSON字符串。 I used JavaScriptSerializer
to parse my student object to JSON. 我使用
JavaScriptSerializer
将学生对象解析为JSON。
namespace MyFirstWcfTestService
{
[ServiceContract]
public interface IService1
{
[OperationContract(Name = "AddParameter")]
[WebGet(UriTemplate = "/std", ResponseFormat = WebMessageFormat.Json)]
String getStudents();
}
}
I always get the following error from OnError
function, and when I look at the response in Chrome development/debug mode I can see my JSON text. 我总是从
OnError
函数中收到以下错误,当我在Chrome开发/调试模式下查看响应时,可以看到我的JSON文本。
Error: success{"readyState":4,"status":200,"statusText":"success"}|"ParserError"|
错误:成功{“ readyState”:4,“状态”:200,“ statusText”:“成功”} |“ ParserError” | "jQuery1706816276672761887_1322704382563 was not called"
“未调用jQuery1706816276676672761887_1322704382563”
My returned json text looks like this : 我返回的json文本如下所示:
"[{\\"StudentID\\":256,\\"StudentFirstName\\":\\"Ali\\",\\"StudentSurname\\":\\"Smith\\"},{\\"StudentID\\":306,\\"StudentFirstName\\":\\"John\\",\\"StudentSurname\\":\\"Menz\\"},{\\"StudentID\\":314,\\"StudentFirstName\\":\\"George\\",\\"StudentSurname\\":\\"Ray\\"},{\\"StudentID\\":316,\\"StudentFirstName\\":\\"Fred\\",\\"StudentSurname\\":\\"Patric\\"},{\\"StudentID\\":603,\\"StudentFirstName\\":\\"George\\",\\"StudentSurname\\":\\"Foster\\"},{\\"StudentID\\":604,\\"StudentFirstName\\":\\"Bobbie\\",\\"StudentSurname\\":\\"Kolman\\"},{\\"StudentID\\":765,\\"StudentFirstName\\":\\"Jim\\",\\"StudentSurname\\":\\"Khas\\"},{\\"StudentID\\":987,\\"StudentFirstName\\":\\"Harry\\",\\"StudentSurname\\":\\"Poter\\"},{\\"StudentID\\":988,\\"StudentFirstName\\":\\"Con\\",\\"StudentSurname\\":\\"Mench\\"},{\\"StudentID\\":1001,\\"StudentFirstName\\":\\"Jim\\",\\"StudentSurname\\":\\"Colon\\"}]"
“ [{\\” StudentID \\“:256,\\” StudentFirstName \\“:\\” Ali \\“,\\” StudentSurname \\“:\\” Smith \\“},{\\” StudentID \\“:306,\\” StudentFirstName \\“ :\\“ John \\”,\\“ StudentSurname \\”:\\“ Menz \\”},{\\“ StudentID \\”:314,\\“ StudentFirstName \\”:\\“ George \\”,\\“ StudentSurname \\”:\\“ Ray \\“},{\\” StudentID \\“:316,\\” StudentFirstName \\“:\\” Fred \\“,\\” StudentSurname \\“:\\” Patric \\“},{\\” StudentID \\“:603,\\” StudentFirstName \\“:\\” George \\“,\\” StudentSurname \\“:\\” Foster \\“},{\\” StudentID \\“:604,\\” StudentFirstName \\“:\\” Bobbie \\“,\\” StudentSurname \\“:\\ “ Kolman \\”},{\\“ StudentID \\”:765,\\“ StudentFirstName \\”:\\“ Jim \\”,\\“ StudentSurname \\”:\\“ Khas \\”},{\\“ StudentID \\”:987,\\ “ StudentFirstName \\”:\\“ Harry \\”,\\“ StudentSurname \\”:\\“ Potter \\”},{\\“ StudentID \\”:988,\\“ StudentFirstName \\”:\\“ Con \\”,\\“ StudentSurname \\” :\\“ Mench \\”},{\\“ StudentID \\”:1001,\\“ StudentFirstName \\”:\\“ Jim \\”,\\“ StudentSurname \\”:\\“ Colon \\”}]“
Would you please help me to find a solution for this problem. 您能帮我找到解决此问题的方法吗? I have already changed my web service from a SOAP to this restful one.
我已经将我的Web服务从SOAP更改为这种宁静的服务。
EDIT I managed to get my jsonp message wraped in an method (myCallback as follow 编辑我设法将我的jsonp消息包装在一个方法中(如下所示的myCallback
myCallback([{"StudentID":256,"StudentFirstName":"Rachel","StudentSurname":"Smith"},{"StudentID":306,"StudentFirstName":"Ali","StudentSurname":"Flemming"},{"StudentID":314,"StudentFirstName":"George","StudentSurname":"Ray"},{"StudentID":316,"StudentFirstName":"Fred","StudentSurname":"Patric"},{"StudentID":603,"StudentFirstName":"George","StudentSurname":"Foster"},{"StudentID":604,"StudentFirstName":"Bobbie","StudentSurname":"Kolman"},{"StudentID":765,"StudentFirstName":"Jim","StudentSurname":"Khas"},{"StudentID":987,"StudentFirstName":"Harry","StudentSurname":"Poter"},{"StudentID":988,"StudentFirstName":"Con","StudentSurname":"Mench"},{"StudentID":1001,"StudentFirstName":"Jim","StudentSurname":"colon"}]);
myCallback([{“ StudentID”:256,“ StudentFirstName”:“ Rachel”,“ StudentSurname”:“ Smith”},{“ StudentID”:306,“ StudentFirstName”:“ Ali”,“ StudentSurname”:“ Flemming”} ,{“ StudentID”:314,“ StudentFirstName”:“ George”,“ StudentSurname”:“ Ray”},{“ StudentID”:316,“ StudentFirstName”:“ Fred”,“ StudentSurname”:“ Patric”},{ “ StudentID”:603,“ StudentFirstName”:“ George”,“ StudentSurname”:“ Foster”},{“ StudentID”:604,“ StudentFirstName”:“ Bobbie”,“ StudentSurname”:“ Kolman”},{“ StudentID “:765,” StudentFirstName“:” Jim“,” StudentSurname“:” Khas“},{” StudentID“:987,” StudentFirstName“:” Harry“,” StudentSurname“:” Poter“},{” StudentID“: 988,“ StudentFirstName”:“ Con”,“ StudentSurname”:“ Mench”},{“ StudentID”:1001,“ StudentFirstName”:“ Jim”,“ StudentSurname”:“ colon”}]));
And I also change my Ajax call as follow 而且我还按照以下方式更改了我的Ajax调用
$.ajax({
type: "post",
url: "http://localhost:52032/Service1.svc/getStd?callback=myCallback", //or callback=?
dataType: "jsonp",
//converters: jQuery.parseJSON,
contentType: 'application/javascript',
success: OnSuccess,
error: OnError
});
But I still get the same error. 但是我仍然遇到同样的错误。 I'm straggling for 3 weeks for such a simple task :(
我为这样一个简单的任务而徘徊了三个星期:(
尝试更改getStudents()以返回Json(List,JsonRequestBehavior.AllowGet)并通过jQuery通过返回的列表分别使用data [iterator] .StudentID,data [iterator] .StudentFirstName等访问返回列表。
Try this changes: 尝试以下更改:
namespace MyFirstWcfTestService
{
[ServiceContract]
public interface IService1
{
[OperationContract(Name = "AddParameter")]
[WebGet(UriTemplate = "/std", ResponseFormat = WebMessageFormat.Json)]
List<StudentDTO>getStudents();
}
class StudentDTO
{
public int StudentID;
public string StudentFirstName;
public string StudentSurname;
}
}
On the client-side: 在客户端:
function OnSuccess(data)
{
for(var i=0;i<data.length;i++)
{
var fn = data[i].StudentFirstName;
var sn = data[i].StudentSurName;
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.