jQuery 移动版上的 $.ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5977872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$.ajax on jquery mobile
提问by Muaz Khan
$.ajax not works properly by using jquery mobile framework...
$.ajax 无法通过使用 jquery 移动框架正常工作...
it just let us downoad html file....
它只是让我们下载 html 文件....
if we want to call 'ActionMethod' then it not works:
如果我们想调用“ActionMethod”,则它不起作用:
$.ajax({
url:'Home/CallMe',
success: function(result) {
alert(result);
} // edited
});
It hangs the system...
它挂系统...
I'm using IPhone Emulator for testing....
我正在使用 iPhone 模拟器进行测试....
Can anyone let me know why above not works and why below works while using jquery mobile framework?
任何人都可以让我知道为什么在使用 jquery 移动框架时上面不起作用以及为什么下面起作用?
$
$
.ajax({
url:'htmlFile.htm',
success: function(result) {
alert(result);
} // this line is edited later
});
Edited: One another thing I want to tell you is that I'm using ASP.NET MVC...
编辑: 我想告诉你的另一件事是我正在使用 ASP.NET MVC ...
Edited:A simplest example of action method that you can try is:
编辑:您可以尝试的最简单的操作方法示例是:
public JsonResult CallMe()
{
return Json("I'm your response");
}
[HttpPost] can also be applied, if you want...
[HttpPost] 也可以应用,如果你想...
回答by Gary Green
Your syntax is incorrect your missing a }
您的语法不正确您缺少一个 }
$.ajax({
url:'controller/action',
success: function(result) {
alert(result);
} // <-- add this
});
回答by Phill Pafford
Hmm, I've used AJAX like this with no problems with jQM. I don't know if you really need to declare a type in the call but I do in my example.
嗯,我用过这样的 AJAX,jQM 没有问题。我不知道你是否真的需要在调用中声明一个类型,但我在我的例子中是这样做的。
$.ajax({
url: 'request.php?page=foo',
type: 'GET',
error : function (){ document.title='error'; },
success: function (data) {
$('#ajax_content').html(data);
}
});
also you could add the data type for a json response as well
您也可以为 json 响应添加数据类型
$.ajax({
url: 'request.php?page=foo',
type: 'GET',
dataType: 'json',
error : function (){ document.title='error'; },
success: function (data) {
alert(data);
}
});
回答by Tanvir Gaus
It's maybe an old post but to make it work from JQM - you need to use Jsonp instead of json due to cross domain issues.
这可能是一篇旧帖子,但要使其从 JQM 工作 - 由于跨域问题,您需要使用 Jsonp 而不是 json。
回答by wordpressm
Known limitations
已知限制
The non-standard environment created by jQuery Mobile's page navigation model introduces some conditions for which you should be aware when building pages:
jQuery Mobile 的页面导航模型创建的非标准环境引入了一些您在构建页面时应该注意的条件:
When linking to directories, without a filename url, (such as href="typesofcats/" instead of href="typesofcats/index.html"), you must provide a trailing slash. This is because jQuery Mobile assumes the section after the last "/" character in a url is a filename, and it will remove that section when creating base urls from which future pages will be referenced.
当链接到没有文件名 url 的目录时(例如 href="typesofcats/" 而不是 href="typesofcats/index.html"),您必须提供尾部斜杠。这是因为 jQuery Mobile 假定 url 中最后一个“/”字符之后的部分是文件名,并且在创建将引用未来页面的基本 url 时,它将删除该部分。
http://demos.jquerymobile.com/1.0a4.1/docs/pages/docs-navmodel.html
http://demos.jquerymobile.com/1.0a4.1/docs/pages/docs-navmodel.html