$.ajax on jquery mobile

浪子不回头ぞ 提交于 2019-12-13 14:18:53

问题


$.ajax not works properly by using jquery mobile framework...

it just let us downoad html file....

if we want to call 'ActionMethod' then it not works:

$.ajax({
   url:'Home/CallMe',
   success: function(result) {
    alert(result);
   }  // edited
});

It hangs the system...

I'm using IPhone Emulator for testing....

Can anyone let me know why above not works and why below works while using jquery mobile framework?

$

.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...

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...


回答1:


Your syntax is incorrect your missing a }

$.ajax({
   url:'controller/action',
   success: function(result) {
    alert(result);
   } // <-- add this
});



回答2:


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({
    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

$.ajax({
    url: 'request.php?page=foo',
    type: 'GET',
    dataType: 'json',
    error : function (){ document.title='error'; }, 
    success: function (data) {
        alert(data);
    }
});



回答3:


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.




回答4:


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:

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.

http://demos.jquerymobile.com/1.0a4.1/docs/pages/docs-navmodel.html




回答5:


Had the same problem with jQuery Ajax call. On pc was working but on mobile devices gave me every time 403 forbidden, with no explanation.

The type: 'GET', saved my day!

Thanks Phill.



来源:https://stackoverflow.com/questions/5977872/ajax-on-jquery-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!