问题描述
请参阅下面的code:
Please see the code below:
<script type='text/javascript'>
$(document).ready(function () {
$("button").click(function(){
alert('Test');
$('#div1 h2').text('Hi I am replace');
var divToBeWorkedOn = "#div1";
var n1 = 1;
var n2 = 2;
var webMethod = "Service1.svc/getNumber";
$.ajax({
type: "POST",
url: webMethod,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
$(divToBeWorkedOn).html(val(result));
},
error: function (e) {
$(divToBeWorkedOn).html(e.responseText);
}
});
})
});
</script>
下面是从服务器端的code:
Here is the code from the server side:
Public Function getNumber() As Integer Implements IService1.GetNumber
Return 1
End Function
该DIV1包含后,我按一下按钮没有任何文字。我认为这是调用Web服务,因为它没有错误,当我把它命名为Service.svc / getNumber错误。什么是我拨错在做什么?
The div1 contains no text after I click the button. I think it is calling the web service because it did error when I named it Service.svc/getNumber by mistake. What am I doing worng?
推荐答案
因为你不beleve,早先code我postad是对的我会加满code我补充道。
I will add the full code I added cuz you dont beleve that the earlier code I postad is right.
伊莫使用Ajax时也应使用JSON。所以,我在code使用JSON在这里,但你也可以只直接返回一个int(我将送我的ajax控制器中的一个ActionResult。
Imo when using Ajax you should also use Json. So I use Json in my code here, but you can also just return an int directly (I will be sending my ajax to an ActionResult in the controller.
控制器:
public ActionResult GetJson()
{
return Json(1);
}
HTML
<div id="div1" style="padding: 5px;"><h2></h2></div>
<button>Click me!</button>
Sctipt:
Sctipt:
<script type='text/javascript'>
$(document).ready(function () {
$("button").click(function(){
alert('Test');
$('#div1 h2').text('Hi I am replace');
var divToBeWorkedOn = "#div1";
var n1 = 1;
var n2 = 2;
$.ajax({
type: "POST",
url: '@Url.Action("GetJson", "Estimate")',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
$(divToBeWorkedOn).html(result);
},
error: function (e) {
$(divToBeWorkedOn).html(e.responseText);
}
});
})
});
</script>
我真的不知道你想用这个做什么,但是这code会做一个警报('测试')则改变文本的div来'我替换,那么该文本将可以与被返回,并放置在结果的值所替代。它将被放置在DIV。
I don't really know what you want to do with this, but this code will do an alert('test') then change the text in the div to 'Hi I am replaced' then that text will be replaced with the value that is returned and placed in result. and It will be placed in the div.
点击之前:
点击后:
如果我已经完全误解你的意思,然后写更多的信息,我会尽力帮助你。
If I have completely misunderstood what you mean, then write more info and I'll try to help you.
希望这有助于
这篇关于JQUERY AJAX没有返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!