3

I am currently trying to retrieve some data from book search sites and populate a personal database with that data. My idea is to inject the necessary jQuery on the page, so that when I see a title I think I'd like to return to in future, I can then just click a cheeckbox, make necessary additional comments, which I then hope to submit by AJAX to a PHP script which then populates my MySQL database for me with the appropriate title.

Do look at this example for a library catalogue:

// 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);    
    }

});

And then I have the 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);





});

My problem is that I can't seem to get the AJAX to work: When I click on my Submit button, I do not see the output I would expect when I used the exact same jQuery on an HTML file I called from localhost. This, which I called using http://localhost:8888/some_html.html worked:

<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 is simply:

echo "Success";

I have read this post: jQuery cannot retrieve data from localhost, which mentions something about "JavaScript cannot currently make direct requests cross-domain due to the Same-origin Policy". Is this the reason why my code didn't work on the external page? If yes, what can I do to make the code work, or what other approaches can I adopt to achieve the same goal? I have mutliple book search sites that I am working on, and many of these do not have an API where I can extract data directly from.

Thank you in advance.

P.S.: I've also tried the suggestion by CG_DEV on How to use type: "POST" in jsonp ajax call, which says that $.post can be done with jsonp, which is the data type to use for cross-domain AJAX. Result: On Firebug I do see the POST request being made. But my function callback is not fired, and firebug doesn't register a Response body when at least "Success" should be returned.

Community
  • 1
  • 1
forgodsakehold
  • 870
  • 10
  • 26

1 Answers1

1

you can set allow cross origin resource sharing Follow two steps: From server set this on response header

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

//* if you want to allow it for all origin domain , or you can specify origin domains also to which you want to allow cors.

In client side add this on your page

$.support.cors = true;

Cons: It is not fully supported on ie < ie10.

Sudhanshu Yadav
  • 2,323
  • 18
  • 18