php - 在外部 URL 上注入(inject)时 jQuery AJAX 无法工作?

标签 php jquery ajax post jsonp

我目前正在尝试从图书搜索网站检索一些数据,并用这些数据填充个人数据库。我的想法是在页面上注入(inject)必要的 jQuery,这样当我看到一个标题时,我想我以后想返回,然后我只需单击一个复选框,进行必要的附加评论,然后我希望提交通过 AJAX 转换为 PHP 脚本,然后用适当的标题为我填充 MySQL 数据库。

请看这个图书馆目录的例子:

// for every book entry, append checkboxes
$('.document-frame').append('<p>Choose:?<input type="checkbox" class="Jcustom_c"  /></p><p>Serendepity?:<input type="checkbox" class="Jserep" /></p><p>Include snippet?:<input type="checkbox" class="Jsnippet" /></p>');

// append a Submit button at the bottom of the page, and a blank div for feedback upon success in POST-ing the necessary data
$('#resultPage').append('<input id="Justin" class="Jcustom" type="submit"/><div id="Jfeedback"></div>');

// whenever my checkbox is checked, retrieve / "scrape" the necessary book data
$('.Jcustom_c').change(function() {


    if ($(this).is(':checked'))
        {

    var title = $(this).parent().parent().find('.title a').text();
    var author = $(this).parent().parent().find('.authors a').text();
        var publishd = $(this).parent().parent().find('.publisher').text();
    var status = $(this).parent().parent().find('.metadata .summary').text();
    var img_link = $(this).parent().parent().find('img.artwork').attr("src")

            //  create an XML string from that data. Escape "<" and ">", otherwise when we append the string to the browser for feedback, the browser will not render them correctly.   
        var appended = '<div class="Jappended">&lt;item&gt;&lt;title&gt;' + title + '&lt;/title&gt;&lt;author&gt;' + author + '&lt;/author&gt;&lt;publisher_n_edn&gt;' + publishd + '&lt;/publisher_n_edn&gt;&lt;status&gt;' + status + '&lt;/status&gt;&lt;image&gt;' + img_link + '&lt;/image&gt;&lt;serep&gt;N&lt;/serep&gt;&lt;/item&gt;</div>';

// show the string just below the book title. Hence if I "pick" the book from the catalogue, the XML string will show up to confirm my the fact that I "picked" it.
        $(this).parent().parent().append(appended);

        }

// if I uncheck the box, I remove the XML string    
    else {

    $(this).parent().nextAll(".Jappended").remove(appended);
    $(this).parent().prevAll(".Jappended").remove(appended);    
    }

});

然后我有 AJAX:

$('#Justin').click(function(e) {

      e.preventDefault;

      var string = "<itemset>";

       $(".Jappended").each(function() {

    var placeh = $(this).text();

    string = string + placeh;
    $('.results_container').append(string);
       })


     // these come from <textarea> boxes I append to the end of the page just before the Submit button. (Above, I did not include the jQuery to append these boxes.)
      var odp = $("#odp").val()
      var mre = $("#motivation_revisit").val()
      var mra = $("#motivation_rationale").val()
      var stu = $(".hdr_block h5 span").text()

      var string = string + "<odpath>" + odp + "</odpath><stused>" + stu + "</stused><motivation><revisit>" + mre + "</revisit><rationale>" + mra + "</rationale></motivation></itemset>"

      var post_var = { xml_string : string, source : "NUS" };

       $.post('http://localhost:8888/db-ajax.php', post_var , function(data) {



          $('#Jfeedback').html(data);





});

我的问题是我似乎无法让 AJAX 工作:当我点击我的提交按钮时,我没有看到我在我调用的 HTML 文件上使用完全相同的 jQuery 时所期望的输出本地主机。我使用 http://localhost:8888/some_html.html 调用的这个有效:

<html>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>

<script>
$(document).ready( function() { 

...

$('#Justin').click(function(e) {

      e.preventDefault;

      var string = "<itemset>";
      /*
       $(".Jappended").each(function() {


      var post_var = { xml_string : "hello", source : "NUS" };

       $.post('http://localhost:8888/db-ajax.php', post_var , function(data) {

       // if (data == "Success") {

          $('#Jfeedback').html(data);


       // }


});

});

});
</script>
<body>

...

</body>

</html>

db-ajax.php 很简单:

echo "Success";

我已阅读这篇文章:jQuery cannot retrieve data from localhost ,其中提到了“由于同源策略,JavaScript 目前无法跨域直接请求”。这是我的代码在外部页面上不起作用的原因吗?如果是,我可以做些什么来使代码工作,或者我可以采用哪些其他方法来实现相同的目标?我正在处理多个图书搜索网站,其中许多网站没有我可以直接从中提取数据的 API。

提前谢谢你。

P.S.:我也试过 CG_DEV 在 How to use type: "POST" in jsonp ajax call 上的建议,它表示 $.post 可以使用 jsonp 完成,jsonp 是用于跨域 AJAX 的数据类型。结果:在 Firebug 上,我确实看到正在发出 POST 请求。但是我的函数回调没有被触发,并且当至少应该返回“成功”时,firebug 没有注册响应主体。

最佳答案

可以设置允许跨源资源共享 遵循两个步骤: 从服务器在响应头上设置这个

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:*

//* 如果你想允许它用于所有源域,或者你也可以指定你想要允许 cors 的源域。

在客户端将此添加到您的页面

$.support.cors = true;

缺点:在 ie < ie10 上不完全支持。

关于php - 在外部 URL 上注入(inject)时 jQuery AJAX 无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16727789/

相关文章:

php - 将 MySQL 数据库连接导入 PhpStorm

php - 如何知道网站在手机或PC浏览器中打开?

javascript - 从js代码赋值给mvc razor的隐藏字段

javascript - 通过 Ajax 加载将 javascript 定义应用于附加字符串

java - 使用 AJAX、JS、JSP 选择复选框时的表格着色

php - AJAX 崩溃,服务器和客户端脚本之间的界限?

php - Laravel 5.1 任务调度命令问题

php - 通过网络从共享文件中读取数据

javascript - 如何使用 AngularJS scope.$watch() 方法观察 JQuery 选择器

jquery - 将 HTML 标签替换为新标签