0

下面的代码工作正常,但我使用 demo.php 来解决跨域问题,我如何在不使用 php 的情况下做到这一点,bcs 客户端不需要 php

 $('#basic-search').submit(function(el){


        var searchdata=$('#search-text').val(); 

            $.ajax({
                  type:"POST" ,
              url:'demo.php',
              data:{
               address: 'http://search-harbinger-n5nyhive6kr72z37m6qrz2xxie.us-east-1.cloudsearch.amazonaws.com/2011-02-01/search?q='+searchdata+'*&results-type=json&return-fields=message&size=10', 
              },
              success:function(response){ 
                if(response!=""){
                response = jQuery.parseJSON(response);
                $.each(response.hits,function(indx,obj){
                    if($.isArray(obj)) {
                        $('.search-results').empty();    
                        $('.search-results').prepend('<div class="basic-results">Results ('+ obj.length+')</div>');
                        if(obj.length!="0"){
                        for(var i=0;i<obj.length;i++){
                            var $temp = '<div class="serach-results-items"><div class=""><b>ID : '+obj[i].id +'</b></div><div class="search-cont"> <b>Message :</b> '+obj[i].data.message+'</div></div>';
                            $('.search-results').append($temp);
                            $('.submit-btn').css('display','none');
                        }
                        }

                        else{
                        $('.search-results').append('<div><div>Your search-<b>'+searchdata+'</b>-did not match any documents.</div><div>Make sure that all words are spelled correctly.</div></div>');
                        $('.submit-btn').css('display','none');
                        }
                    }
                });

                }
                else{alert('something went wrong.');}
             }
            });

        });
4

1 回答 1

0

你应该使用 jsonp 进行跨域调用,像这样

    $.ajax({
          type:"POST" ,
          url:'demo.php',
          data:{
           address: 'http://search-harbinger-n5nyhive6kr72z37m6qrz2xxie.us-east-1.cloudsearch.amazonaws.com/2011-02-01/search?q='+searchdata+'*&results-type=json&return-fields=message&size=10', 
          }
         dataType: 'jsonp',
         jsonp: 'callback', 
于 2013-04-09T09:02:56.200 回答