我在C#中创建了一个Web API,并从jquery调用该api,但收到错误:
('#buttonid').click(function (event) {
$.ajax({
method: "GET",url: "http://localhost:54580/api/persons",
dataType: 'jsonp',
beforeSend: function (request) {
request.setRequestHeader("Authorization", "Negotiate");
},
async: true,
success: function (data) {
console.log(JSON.stringify(data));
},
error: function(jqXHR,error, errorThrown) {
if(jqXHR.status&&jqXHR.status==400){
console.log(jqXHR.responseText);
}else{
console.log("Something went wrong");
}
}
});
});
在调试模式下,我能够看到我的数据。:
http://localhost:54580/api/persons?callback=jQuery111103874004708615564_1461475734608&_=1461475734609
数据是:
[{
"FirstName": "Ramu",
"LastName": "Rai",
"Email": "test123"
}, {
"FirstName": "Ramu1",
"LastName": "Rai1",
"Email": "test123"
}]
但代码永远不会成功。它总是打印出来#34;出了问题"
答案 0 :(得分:0)
使用json
代替jsonp
,尝试下面的演示(替换网址)
$.ajax({
method: "GET",
url: "http://jsonplaceholder.typicode.com/posts/1", //"http://localhost:54580/api/persons",
beforeSend: function(request) {
request.setRequestHeader("Authorization", "Negotiate");
},
success: function(data) {
var result = JSON.stringify(data);
var $result = document.querySelector('#pre');
$result.textContent = result;
},
error: function(jqXHR, error, errorThrown) {
if (jqXHR.status && jqXHR.status == 400) {
console.log(jqXHR.responseText);
} else {
console.log("Something went wrong");
}
}
});
function my_function(data){
console.log('my_function', data);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="pre"></pre>
&#13;