简体   繁体   English

使用输入onBlur触发跨域YQL ajax请求

[英]Trigger cross domain YQL ajax request with input onBlur

I have a form to collect information about a product (ie from Amazon). 我有一个表单来收集有关产品的信息(即来自亚马逊)。 I am attempting to trigger a YQL ajax request on blur of the URL input. 我试图在URL输入模糊时触发YQL ajax请求。 Currently, no errors in console, but no results either. 目前,控制台没有错误,但也没有结果。 Here is my form input: 这是我的表单输入:

<div class="uk-form-row">
            <div class="uk-form-label"><?php echo $this->form->getLabel('item_url'); ?></div>
            <div class="uk-form-controls "><input type="text" name="jform[item_url]"   id="jform[item_url]" value="<?php if (!empty($this->item->id)) { echo $this->item->item_url;}; ?>" class="uk-form-large uk-width-medium-1-1" placeholder="http://" aria-required="required" required="required"/></div>
          </div> 
              <script type="text/javascript"> 
    jQuery( "#jform[item_url]" ).blur(function() {
              var path = jQuery('#jform[item_url]').val();
          requestCrossDomain(path, function(results) {
   jQuery('#url_results').html(results);
    });
    });
    </script>

    <div id="url_results">
          </div>

My function: 我的功能:

    // Accepts a url and a callback function to run.
function requestCrossDomain( site, callback ) {

    // If no url was passed, exit.
    if ( !site ) {
        alert('No site was passed.');
        return false;
    }

    // Take the provided url, and add it to a YQL query. Make sure you encode it!
    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=cbFunc';

    // Request that YSQL string, and run a callback function.
    // Pass a defined function to prevent cache-busting.
    jQuery.getJSON( yql, cbFunc );

    function cbFunc(data) {
    // If we have something to work with...
    if ( data.results[0] ) {
        // Strip out all script tags, for security reasons.
        // BE VERY CAREFUL. This helps, but we should do more. 
        data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');

        // If the user passed a callback, and it
        // is a function, call it, and send through the data var.
        if ( typeof callback === 'function') {
            callback(data);
        }
    }
    // Else, Maybe we requested a site that doesn't exist, and nothing returned.
    else throw new Error('Nothing returned from getJSON.');
    }
}

Here's the fiddle: http://jsfiddle.net/FLY66/2/ 这是小提琴: http//jsfiddle.net/FLY66/2/

This issue is more related to your server. 此问题与您的服务器更相关。 You can simply set the Access-Control-Allow-Origin header on your server. 您只需在服务器上设置Access-Control-Allow-Origin标头即可。 Look for your server language to see how to set Access-Control-Allow-Origin 查找您的服务器语言,了解如何设置Access-Control-Allow-Origin

Setting it to * will accept cross-domain AJAX requests from any domain. 将其设置为*将接受来自任何域的跨域AJAX请求。 Another alternative is use 'JSONP' data type for returned data. 另一种方法是使用'JSONP'数据类型返回数据。

References 参考

http://en.wikipedia.org/wiki/Same_origin_policy http://en.wikipedia.org/wiki/Same_origin_policy

https://developer.mozilla.org/en/http_access_control https://developer.mozilla.org/en/http_access_control

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM