jquery (ajax) redirect to another domain

后端 未结 3 837
陌清茗
陌清茗 2021-01-17 06:18

I have the jquery json request in the following format:

JSON request:

$.getJSON(\'http://xyz.com\',function(result) {});

If the req

相关标签:
3条回答
  • 2021-01-17 06:40

    Did you try this?

    function addImage(item) {
     $("<img/>").attr("src", item.media.m).appendTo("#images");
    }
    
        var jqxhr = $.getJSON("http://xyz.com",function(data) {
        $.each(data.items, function(i,item){  //sample
           addImage(item);
        })
        .error(function() { 
                  var jqxhrFailover = $.getJSON("http://zzz.com", function(data) {
        $.each(data.items, function(i,item){  //sample
           addImage(item);
        }).error(function(){ alert('both failed!'); });
    
    0 讨论(0)
  • 2021-01-17 06:47

    $.getJSON() is defined as:

    jQuery.getJSON=function(url,data,callback){
        return jQuery.get(url,data,callback,"json");
    };
    

    and as such, fails silently.

    To react to errors you'll need to use the $.ajax() function directly which allows you to define an onError handler, as in:

    $.ajax({
        url: '...',
    
        contentType:'json',
    
        success:function(result,stat,xhr){
            ...
        },
    
        error:function(xhr,opts,err){
            ...
        }
    });
    

    See jQuery Ajax error handling, show custom exception messages for further information.

    0 讨论(0)
  • 2021-01-17 07:06

    I think that it may be better to use $.ajax(), error method or success if the server tells you based on the response).

    Then use this to redirect the browser.

    window.location = "http://www.google.com"
    
    0 讨论(0)
提交回复
热议问题